Rails CRUD API requests with cURL
cURL let’s you make HTTP requests from the command-line/terminal.
You can make API CRUD requests to a Rails controller via cURL.
Let’s create a Post
scaffold and make some CRUD requests:
rails g scaffold Post title:string body:text published:boolean published_at:datetime
READ #
index
curl -X GET http://localhost:3000/posts.json
show
curl -X GET http://localhost:3000/posts/1.json
CREATE, UPDATE, DELETE #
If you try performing Create
, Update
, Delete
actions, you will get a ActionController::InvalidAuthenticityToken
error due to RequestForgeryProtection.
Add protect_from_forgery with: :null_session
to your controller to enable these requests:
# app/controllers/posts_controller.rb
# skip_before_action :verify_authenticity_token
# skip_before_action :authenticate_user!
protect_from_forgery with: :null_session
Important: now anybody on the internet will be able to make changes to your database. In a real environment, you would want to add an authentication layer to your cURL requests.
create
curl -X POST -H "Content-Type: application/json" -d '{"post": {"title": "Example Title", "body": "Example Body", "published": true, "published_at": "2023-04-12T12:34:58Z"}}' http://localhost:3000/posts.json
update
curl -X PATCH -H "Content-Type: application/json" -d '{"post": {"title": "Updated Title"}}' http://localhost:3000/posts/1.json
destroy
curl -X DELETE http://localhost:3000/posts/1.json
Did you like this article? Did it save you some time?