I’m trying to send a POST request from an external Ruby script to a Rails app via HTTP#post_form. The request is made to the create action (i.e. the URI is http://server/controller).
If I encode a single parameter into the request, everything is fine:
HTTP::post_form(uri, { :my_param => "value" })
Though I do have to explicitly pull out my_param from params manually, in the controller. This seems inefficient, and breaks creating a new record from within the app itself (because that parameter is not there). I’m consequently trying to make my script pose as Rails itself, passing the appropriate data as the controller would expect it, e.g.
HTTP::post_form(uri, { :object => { :my_param => "value" } })
However, this doesn’t work. post_form seems to be escaping my hash into something different, i.e.
{ "object" => [\"my_param\", \"value\"] }
Which obviously doesn’t do the same thing. Am I missing something obvious in the way I’m passing the data? Or can I not achieve what I’m after (creating a new record from outside the app)?
One straightforward way might be to simply imitate how Rails formats its parameters, like this:
Edit: Well, look at that, I looked around a bit and found that Rails actually gives you a method to do the same thing using their own mechanism:
The
to_parammethod will treat Arrays correctly, and everything else too. Notice that in this case you want to useHTTP::post, notpost_form, since the parameters are already converted to a string.