Gyanendra Singh
Gyanendra Singh

Reputation: 1005

POST request in rails

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.

Upvotes: 1

Views: 5899

Answers (2)

brg
brg

Reputation: 3953

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

Upvotes: 1

PinnyM
PinnyM

Reputation: 35541

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.

Upvotes: 0

Related Questions