1

I'd like to be able to inspect the params hash before all of the values are stringified by Rails. For example if I am using application/json Accept/Content-Type, and I receive:

{ "id":1, "post":"Hello" }

I want to be able to know that params[:id] was originally passed as a JSON integer, not a string.

I also want to be able to do this within a controller spec, which uses a limited set of middleware (or none at all?). Is this possible?

Logan Serman
  • 27,645
  • 25
  • 100
  • 139
  • I believe that all values are passed as Strings in the URL and/or form data and are implicitly converted to their respective types by Ruby. So this might not be easily achievable. – Alexander May 01 '15 at 19:07
  • Is there any particular reason you want to know it was passed as a JSON integer? If you are expecting an integer for a field, then you can cast it using the `to_i` method. – Alexander May 01 '15 at 19:13
  • Can you explain why you need to know the difference between '1' and 1? You can just cast one type to another, and usually ActiveRecord will do this for you – bukk530 May 01 '15 at 19:22
  • Yes there is a reason, no I will not explain it because it is the spec of a personal project which has no relevance to this question. – Logan Serman May 02 '15 at 14:06

1 Answers1

0

I believe this post has what you are looking for: How to access the raw unaltered http POST data in Rails?

request.raw_post

http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-raw_post

Community
  • 1
  • 1
house9
  • 19,614
  • 8
  • 52
  • 60
  • This method only works for POST requests. On any other type of request, it returns an empty String. – Alexander May 01 '15 at 19:11
  • Sending a body in a GET request should not be needed and is highly discouraged. Further discussion on this: http://stackoverflow.com/a/983458/2430657. Either way, `request.body` and `request.body.read` would print out the values as a String. – Alexander May 01 '15 at 19:29
  • http://stackoverflow.com/questions/6441701/get-raw-params-rather-than-the-processed-into-hashes-versions?answertab=active#tab-top - it looks like you will have to parse it out of `request.url` for GET request – house9 May 01 '15 at 19:36
  • Sadly it looks like there is no `raw_put` either :( – Logan Serman May 02 '15 at 14:08