Reading data from request body

35 views
Skip to first unread message

Panayotis Matsinopoulos

unread,
Aug 7, 2015, 11:08:30 AM8/7/15
to Ruby on Rails: Talk
Hi Everyone,

In one of my controller actions, I am using

data_sent = ActiveSupport::JSON.decode(request.body.read)

to get the JSON object sent to this controller via request that has the object/payload inside the body. I am using curl to send/test this, as follows:

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" - -d "{\"foo\": \"bar\"}" "http://localhost:3000/foo"

The result is that I get in data_sent a Hash like this: {foo: "bar"}, which seems to be correct.

The question here is whether the way I get the data on controller is the recommended way to do it, or whether there is other more Rails/better way to do it.

Thanks in advance
Panayotis

tamouse pontiki

unread,
Aug 7, 2015, 4:17:49 PM8/7/15
to rubyonra...@googlegroups.com
On Fri, Aug 7, 2015 at 10:08 AM, Panayotis Matsinopoulos <pana...@matsinopoulos.gr> wrote:
The question here is whether the way I get the data on controller is the recommended way to do it, or whether there is other more Rails/better way to do it.


The data is also available in params:

    params[:foo] == "bar"

The (potential) problem is that params will also contain additional fields added by ActionDispatch: params[:controller] and params[:action], so it won't be *only* the parsed request body. 
 

Panayotis Matsinopoulos

unread,
Aug 8, 2015, 2:32:14 AM8/8/15
to Ruby on Rails: Talk
Hmmmmm. Yes, It works indeed as you say.Thanks for that. To tell you truth, I tried that from the beginning, but it didn't work for me. But probably was 
error on the client side not sending the correct request, rather than on the server side.

Thanks a lot.
Best
Panayotis

Scott Ribe

unread,
Aug 8, 2015, 8:49:47 AM8/8/15
to rubyonra...@googlegroups.com, Panayotis Matsinopoulos
On Aug 8, 2015, at 12:32 AM, Panayotis Matsinopoulos <pana...@matsinopoulos.gr> wrote:
>
> Hmmmmm. Yes, It works indeed as you say.Thanks for that. To tell you truth, I tried that from the beginning, but it didn't work for me. But probably was
> error on the client side not sending the correct request, rather than on the server side.

In the future when you have a problem like that, first thing to do is log params.inspect and take a look. Maybe you'll see how the params are, and understand that you had a mistake in the way you were accessing them. If not, then log the request body, and see how RoR parses a request body into params, and understand that maybe you were expecting the wrong thing there, or if not, that the request body is not correct.

When you work with complex forms, it's not at all uncommon to not find data where you expect it in the params when you're first developing. So getting comfortable with debugging those issues is a good thing ;-)

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice





Reply all
Reply to author
Forward
0 new messages