Rails active resource: how to send post parameters in the request body and not as query strings?

3.7k views Asked by At

I am calling some post request using rails activeresource, however all the parameters are sent as query string and the result is that the called url is too long and I get WEBrick::HTTPStatus::RequestURITooLarge exception.

So I need to send the parameters in the request body instead, however I couldn't find how to do this.

Thanks a lot

3

There are 3 answers

1
khaled_gomaa On BEST ANSWER

To send a post request in activeresource you should reference the documentation

For example you can do this

#Entity.post(custom_method_name, options = {}, body = '')
Company.post(:add_role, nil, {user_id: 1, role_id: 2}.to_json)

Tell me if you need anything else.

0
Rubyrider On

So, I think you are doing the post request within a link. Anchor by default will create the query parameter. But if you use button and that does send the data by default in post method send as request body like a form of post method. Please try out the following the code snippets:

button_to 'Your post requesting link name', something_path(:your_params => :will be here) 

Let me know if this works!

Thanks.

1
Mohamed Yakout On

Try to install thin server instead of Webrick, read these answer-1, answer-2.

Or, Add webrick.rb file to config\initializers directory, and add the following code:

if defined?(WEBrick::HTTPRequest)
  WEBrick::HTTPRequest.const_set("MAX_URI_LENGTH", 10240)
end

Read also this answer.