I want to send a POST request to a rails application and have it save and parse the request body in the database...
My route on the receiving end is currently setup as:
post '/request' => 'controller#receives_data'
when I post data to this controller I use:
def post_it connection.post(uri.path, "this is data", header_with_authkey) end
My controller method that receives the post is setup as:
def receives_data log(request.body.read) end
However I am getting a 422 error, unprocessable entity
, and the log file is always empty...
Are there specific headers I need to include to post this to a rails app? Are there specific configurations I need to include in my controller or routes?
We use POST to create a new resource. A POST request requires a body in which you define the data of the entity to be created. A successful POST request would be a 200 response code.
The format of an HTTP POST is to have the HTTP headers, followed by a blank line, followed by the request body. The POST variables are stored as key-value pairs in the body. You can see this using a tool like Fiddler, which you can use to watch the raw HTTP request and response payloads being sent across the wire.
The biggest difference is that GET puts all the data in the URL (which can be limited in size), while POST sends it as a data part of the HTTP request.
After the DNS gets resolved, the request hits a web server, which asks Rails what it has for that url . Rails goes to the routes. rb file first, which takes the URL and calls a corresponding controller action. The controller goes and gets whatever stuff it needs from the database using the relevant model .
request.raw_post
Read the request body. This is useful for web services that need to work with raw requests directly.
http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-raw_post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With