POST request using CURL for Rails application
This question is asked by many users and I tried all solutions but none of them worked. for ex: this question is here curl json post request via terminal for rails app , but still the same result.
I am trying to POST data through the terminal using CURL by running the following command:
curl -i -H 'Authorization: Token token'="9asdadsasd87t8ddaghsd" -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"request":{"sender_mobile":"12331212","recipient_mobile":"121231231","location":"Bangalore"}}' http://localhost:3000/api/v1/requests
and Getting a response:
HTTP/1.1 422 Unprocessable Entity
Content-Type: text/html; charset=utf-8
Content-Length: 15450
X-Request-Id: f1025e69-9ff3-4bd1-9bd5-0679fbcc50bc
X-Runtime: 0.062784
Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
Date: Thu, 08 Jan 2015 10:35:45 GMT
Connection: Keep-Alive
with so many lines of garbage, but I think this answer is enough to understand what might be causing it. Here is a link to the complete error http://pastebin.com/n342HeYL
I can successfully complete a GET request with the command:
curl -i 'http://localhost:3000/api/v1/requests' -H 'Authorization: Token token'="952aa4598ec2cd87c8b69056616a00af"
Answer:
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: application/json; charset=utf-8
Etag: "b956523e0345d2bb466d39823195b600"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 91d21235-246f-4557-a67a-92dca02b9cc1
X-Runtime: 0.011781
Server: WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)
Date: Thu, 08 Jan 2015 10:51:55 GMT
Content-Length: 486
Connection: Keep-Alive
So, I don't know what happened to the POST request.
Here is my request_controller.rb file
module Api
module V1
class RequestsController < ApplicationController
# skip_before_action :verify_authenticity_token
before_filter :restrict_access
respond_to :json
def index
respond_with Request.all
end
def show
respond_with Request.find(params[:id])
end
def create
respond_with Request.create(params[:request])
end
def update
respond_with Request.update(params[:id], params[:request])
end
def destroy
respond_with Request.destroy(params[:id])
end
private
def restrict_access
authenticate_or_request_with_http_token do |token, options|
ApiKey.exists?(access_token: token)
end
end
end
end
end
source to share
Regarding the conversation above, let me write down the answer.
If you see a shape in rails, it will have the line shown below
<input name="authenticity_token" type="hidden" value="+wFCV3kv0X/WI0qb54BgYlDzi+Tp+6HIGM61a4O6gg0=">
Now if ApplicationController
you have this line in you protect_from_forgery
then it will always wait authenticity_token
, and if not available it will throw the error indicated in your logs, so I suggested removing this line because you will not be passing authenticity_token
as a parameter from yours api requests POST
.
The reason your fetch request works very well is because rails do not expect authenticity_token
on request GET
.
Now you said you deleted protect_from_forgery
, but you got an internal server error that should be handled by you since it has nothing to do with the request GET
or POST
, but this is a bug in your rails app. So resolve this error and it should work fine. Or also post the error log here so I can help you with that.
EDIT: Solving the 500 backend server problem
With the logs nested below it is also necessary that you allow attributes in your controller, just like @abhinay mentioned in the code.
Hope it helps
source to share