1

I need to create a post request in a somewhat weird(speaking leniently) format. The exact request to be sent should be in the following format

https://xyz.com/ping?app_id= 123&adv_id=345&event=sale&event_data="amt=30_USD;user_id=204050"

Its easy to send a post request to an url of the following format :-

https://xyz.com/ping?app_id= 123&adv_id=345&event=sale&amt=30_USD&user_id=204050

This can be achieved using code like this :-

Net::HTTP.post_form(URI.parse("http://xyz.com/ping"), params)

where, the params variable is appropriately populated(hash).

What modification should i make to account for this change from normal scenario, particularly to account for the double quotes around event data.

Gyanendra Singh
  • 895
  • 2
  • 13
  • 30
  • Why are you using `post_form`? Does this need to be as a POST request? The format you are aiming for is actually used by GET requests, while POST parameters are not part of the URL, but rather embedded in the form body – PinnyM Jan 08 '13 at 20:56
  • I need to make a post request(preferably, since i'm pinging another site with some data). Also, even if I were to make a get request, how would this work? – Gyanendra Singh Jan 08 '13 at 21:09

2 Answers2

1

Read and adapt the information from the links below. After going through them, I can deduce 4 possible ways to do this:

  1. Use Mechanize. See link 1
  2. Do a post request from your controller using Net::HTTP. See link 2 - 3(3rd answer).
  3. Post form data containing a hash or array. See links 4 - 7
  4. Add hidden field to your form that will contain the extra data. See link 8
  5. Use the params merge pattern ie Link 9

Using Ruby/Rails To Programmatically Post Form Data To Another Site

Submitting POST data from the controller in rails to another website

Post and redirect to external site from Rails controller?

http://rails.nuvvo.com/lesson/6371-action-controller-parameters

http://www.developer.com/lang/rubyrails/article.php/3804081/Techniques-to-Pass-and-Read-URL-Parameters-Using-Rails.htm

http://guides.rubyonrails.org/action_controller_overview.htm (the section 3.1 Hash and Array Parameters, then section 8 on Request Forgery Protection)

Rails: How do I make a form submit an array of records?

Ruby on Rails: Submitting an array in a form

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-hidden_field

Send querystring params as part of form post

Community
  • 1
  • 1
brg
  • 3,915
  • 8
  • 37
  • 66
0

You need to make sure that the event_data parameter is properly escaped to do this. I'm pretty sure that calling post_form will do this for you already.

params["app_id"] = 123
params["adv_id"] = 345
params["event"] = 'sale'
params["event_data"] = '"amt=30_USD;user_id=204050"'
Net::HTTP.post_form(URI.parse("http://notify.tapsense.com/ads/ping"), params)

That should more or less do it for you.

PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • How should I test the actual url formed, since this is post request is fired from the server side. – Gyanendra Singh Jan 09 '13 at 07:16
  • @user523146: I don't see an easy way to do this without mocking, as you'll need to inspect the internals of the HTTPRequest. Which mocking framework would you use? – PinnyM Jan 09 '13 at 15:07
  • I have no idea. I tried to see if ActiveResource::HttpMock would work, but it does not look like that. – Gyanendra Singh Jan 10 '13 at 20:09
  • If you can use RSpec, then [this example](http://stackoverflow.com/questions/13755452/how-to-mock-nethttppost) would work for you. – PinnyM Jan 10 '13 at 20:34
  • I'll try that, when i inspect the post_form method, it actually encode the event_data params. As a result, the final url will be half encoded(the "=" and "," characters in event_data are replaced by %xx ). – Gyanendra Singh Jan 11 '13 at 11:09
  • Yes it will indeed be encoded as per the HTTP spec, and it will be decoded at the other end. And these won't be sent as part of the URL (POST requests never are) - they'll be sent as part of the form body. – PinnyM Jan 11 '13 at 13:45
  • The params data in event_data is encoded. However, the other params are not encoded. As a result, half of the query string parameters are encoded, and the other half(all barring event_data) are not. Is this not a case wherein the url itself is not consistent? – Gyanendra Singh Jan 14 '13 at 10:38
  • All params are encoded, but encoding won't change the passed values where only 'normal' characters are present. So yes, this is expected behavior and the URL/form-data is indeed consistent with the HTTP encoding standard. – PinnyM Jan 14 '13 at 15:19