Request body params

642 views
Skip to first unread message

Janko Marohnić

unread,
Apr 13, 2015, 10:49:23 PM4/13/15
to ruby...@googlegroups.com
My application provides a JSON API, and on post requests it includes JSON data in the request body. Is there some convenient way to access them in Roda, but already parsed? In Sinatra, Rails and Grape `params` would automatically merge the request body parameters to itself (parsed as either JSON or form-urlencoded), so you'd already have everything set up.

Michel Benevento

unread,
Apr 14, 2015, 2:55:52 AM4/14/15
to ruby...@googlegroups.com
My rest_api plug-in does what you want. See http://github.com/beno/roda-rest_api


On 14 apr. 2015, at 04:49, Janko Marohnić <janko.m...@gmail.com> wrote:

My application provides a JSON API, and on post requests it includes JSON data in the request body. Is there some convenient way to access them in Roda, but already parsed? In Sinatra, Rails and Grape `params` would automatically merge the request body parameters to itself (parsed as either JSON or form-urlencoded), so you'd already have everything set up.

--
You received this message because you are subscribed to the Google Groups "Roda" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-roda+...@googlegroups.com.
To post to this group, send email to ruby...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-roda.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-roda/e8d07c66-28ec-4ea0-ad20-6f656557149f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Janko Marohnić

unread,
Apr 14, 2015, 5:14:19 AM4/14/15
to ruby...@googlegroups.com
Thanks, this looks like a very useful plugin. I see from the source code that the params are parsed when you use one of the resourceful routes (#index, #show, #create etc.), but it doesn't seem to do that on regular routes. Is it possible to use non-resourceful routes (like I have above), and still get the request body parsing?

You received this message because you are subscribed to a topic in the Google Groups "Roda" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-roda/5gRDIMqsfMc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-roda+...@googlegroups.com.

To post to this group, send email to ruby...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-roda.

Michel Benevento

unread,
Apr 14, 2015, 6:35:44 AM4/14/15
to ruby...@googlegroups.com
The plugin doesn’t do parsing on regular routes like you said, so you’ll have to roll your own...

Michel


Jeremy Evans

unread,
Apr 14, 2015, 11:21:50 AM4/14/15
to ruby...@googlegroups.com
On Monday, April 13, 2015 at 7:49:23 PM UTC-7, Janko Marohnić wrote:
My application provides a JSON API, and on post requests it includes JSON data in the request body. Is there some convenient way to access them in Roda, but already parsed? In Sinatra, Rails and Grape `params` would automatically merge the request body parameters to itself (parsed as either JSON or form-urlencoded), so you'd already have everything set up.

People have asked about this in the past, and I still think the middleware approach used by https://github.com/achiu/rack-parser is the best way to handle this.

However, since a lot of people seem to have this need, it may be worth shipping a smaller and lighter weight plugin that does the same thing.  I'll see if I can get that in before the next release.

Thanks,
Jeremy

Janko Marohnić

unread,
Apr 14, 2015, 11:48:40 AM4/14/15
to ruby...@googlegroups.com
I'm wondering what is the best way to do expose this. It would be ideal if the method was named `params`, but then it would clash with the "indifferent_params" plugin. On the other hand, I feel like rack-parser's `env["rack.request.form_hash"]` would not be ideal to write. Also, I don't like in that rack-parser named the request body JSON "form_hash", becuase it doesn't come from a form (forms are url encoded), and even if it could, it doesn't have to (it can also come from an AJAX call).

What do you think about merging that data into `Roda::RodaRequest#params`? This way plugins like "indifferent_params" would automatically work correctly.

--
You received this message because you are subscribed to a topic in the Google Groups "Roda" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-roda/5gRDIMqsfMc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-roda+...@googlegroups.com.
To post to this group, send email to ruby...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-roda.

Jeremy Evans

unread,
Apr 14, 2015, 2:01:00 PM4/14/15
to ruby...@googlegroups.com
On Tuesday, April 14, 2015 at 8:48:40 AM UTC-7, Janko Marohnić wrote:
I'm wondering what is the best way to do expose this. It would be ideal if the method was named `params`, but then it would clash with the "indifferent_params" plugin. On the other hand, I feel like rack-parser's `env["rack.request.form_hash"]` would not be ideal to write. Also, I don't like in that rack-parser named the request body JSON "form_hash", becuase it doesn't come from a form (forms are url encoded), and even if it could, it doesn't have to (it can also come from an AJAX call).

What do you think about merging that data into `Roda::RodaRequest#params`? This way plugins like "indifferent_params" would automatically work correctly.

The approach I've chosen overrides RodaRequest#POST, which is what gets merged into GET to result in params. So it will work with indifferent_params.  It's already fully written, but I want to do some real world testing with third party software before committing it.

Thanks,
Jeremy

Janko Marohnić

unread,
Apr 14, 2015, 2:03:07 PM4/14/15
to ruby...@googlegroups.com
Awesome, thank you, Jeremy :)

--
You received this message because you are subscribed to a topic in the Google Groups "Roda" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-roda/5gRDIMqsfMc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-roda+...@googlegroups.com.
To post to this group, send email to ruby...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-roda.

Jeremy Evans

unread,
Apr 14, 2015, 2:23:33 PM4/14/15
to ruby...@googlegroups.com
On Tuesday, April 14, 2015 at 11:03:07 AM UTC-7, Janko Marohnić wrote:
Awesome, thank you, Jeremy :)

It's available now, please try it out and let me know what you think:


Thanks,
Jeremy

Janko Marohnić

unread,
Apr 14, 2015, 2:37:18 PM4/14/15
to ruby...@googlegroups.com
Works perfectly, thank you.

--
You received this message because you are subscribed to a topic in the Google Groups "Roda" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-roda/5gRDIMqsfMc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-roda+...@googlegroups.com.
To post to this group, send email to ruby...@googlegroups.com.
Visit this group at http://groups.google.com/group/ruby-roda.
Reply all
Reply to author
Forward
0 new messages