Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving POST data in Rails 4 and reading request.body

Tags:

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?

like image 491
Davey Avatar asked Nov 12 '13 02:11

Davey


People also ask

Does POST request have body?

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.

How do you send a request body in POST method?

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.

What is the difference between a post request and a GET request rails?

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.

How a request is processed in rails?

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 .


1 Answers

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

like image 168
feipinghuang Avatar answered Sep 28 '22 10:09

feipinghuang