Receiving Post Data in Rails 4 and Reading Request. Body

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?

2

2 Answers

request.raw_post

Read the request body. This is useful for web services that need to work with raw requests directly.

You'll need to set the following headers in your post.

Content-Type: application/json
Accept: application/json
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest