Re: Request with Content-Type: "application/json" add extra parameter.

410 views
Skip to first unread message

Frederick Cheung

unread,
Apr 18, 2013, 3:24:13 AM4/18/13
to rubyonra...@googlegroups.com
On Thursday, April 18, 2013 5:10:14 AM UTC+1, rob...@capansis.com wrote:
> Howdy,
>
>

>
> Is there any way I can just turn off this functionality?  I think it might be because this is an EventsController, and Rails is trying to give me something for free, but I just want to send over whatever I want, especially as I will have situations like an OrdersController, that I want to pass an order to (such as id asc, etc..).
>
>

There are 2 things happening. First, by default if the content type is json then rails assumes that the request body (in your case the empty string) is json, and will parse it and add it to params.

The second is something called wrap_parameters.
This wraps the parameters from the body in a hash, so if you posted the document

{"name": "bob"}

To a users controller, instead of polluting the top level parameter namespace it would set params[:user] to the result of parsing that.

There is an initializer that turns on wrap parameters. You could remove it and/or only enable it for some controllers.

By default this only happens if the content type is json.

Lastly why are you setting the content type if you're not submitting the request body (if you're trying to control the format of the response I find that it's easiest not to mess around with headers and requests events.json instead)

Fred
> Thanks for any help,
>
>
> Robert

rob...@capansis.com

unread,
Apr 18, 2013, 3:19:09 PM4/18/13
to rubyonra...@googlegroups.com
Hi Frederick,

Thanks for the help, disabling wrap_parameters in my initializer fixed my problem.  Reason why I am setting the content type for the GET is just because I have jQuery to always ask for JSON.  I could probably set it to only send the header on POST/PUT requests, but I would still have the problem were params would get clobbered in my post body anyway.

Really appreciate the help!

Trigger Woods

unread,
Jan 24, 2014, 3:30:16 AM1/24/14
to rubyonra...@googlegroups.com
Is there meaning of adding a check of method type (GET or POST/PUT) to ParamsWrapper#_wrapper_enabled?

For example:


def _wrapper_enabled?
       return false if request.get?
       ref = request.content_mime_type.try(:ref)
       _wrapper_formats.include?(ref) && _wrapper_key &&              !request.request_parameters[_wrapper_key]
end

Matt Jones

unread,
Jan 25, 2014, 1:16:14 PM1/25/14
to rubyonra...@googlegroups.com


On Thursday, 18 April 2013 15:19:09 UTC-4, rob...@capansis.com wrote:
Hi Frederick,

Thanks for the help, disabling wrap_parameters in my initializer fixed my problem.  Reason why I am setting the content type for the GET is just because I have jQuery to always ask for JSON.  I could probably set it to only send the header on POST/PUT requests, but I would still have the problem were params would get clobbered in my post body anyway.


Content-Type specifies what format the *request* is in (the data you are sending). For GET requests, it's not particularly meaningful - there isn't a "request body", so there's nothing to specify the format of.

If you want to specify what format you'd like the *response* in, use the Accept header.


--Matt Jones 
Reply all
Reply to author
Forward
0 new messages