Cainus
(Cainus)
August 17, 2011, 12:59am
1
Hey all... I'm building a RESTful JSON API and I find myself writing
this:
JSON.parse(request.body.read)
...over and over in every controller action that accepts JSON in the
request body (which is a lot of actions).
Does rails have a better built-in way to keep the code more DRY? Or
should I solve that somehow myself?
Thanks,
Cainus
Hey all... I'm building a RESTful JSON API and I find myself writing
this:
JSON.parse(request.body.read)
...over and over in every controller action that accepts JSON in the
request body (which is a lot of actions).
Does rails have a better built-in way to keep the code more DRY? Or
should I solve that somehow myself?
If the content-type is set properly on the request, rails should
populate params with the parsed json itself.
Fred
You can also use to_josn method to get the json string. Its built-in method in rails.
when request arrives at server you can get json data in parameter which you can fatch by params[:]
You can view the whole request ad json in rails by using req.as_json method.