Reputation: 9184
Where should I URL-encode my POST request data? For each param, or for the whole query line?
For example, I have this code:
@VIEWSTATE = url_encode(@VIEWSTATE)
data = ("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=#{@VIEWSTATE}&__EVENTVALIDATION=#{@EVENTVALIDATION}&=ctl0.......
headers = {
'Cookie' => cookie,
***
}
resp, data = http.post(path, data, headers)
Is it right, or must I write:
@VIEWSTATE = @VIEWSTATE
data = url_encode("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=#{@VIEWSTATE}&__EVENTVALIDATION=#{@EVENTVALIDATION}&=ctl0.......
headers = {
'Cookie' => cookie,
***
}
resp, data = http.post(path, data, headers)
Which is right, and which is better?
Upvotes: 0
Views: 2252
Reputation: 160631
I generally like to use URI's encode_www_form
which takes either an array or a hash of name/value pairs. This is the example from the documentation:
require 'uri' URI.encode_www_form([["q", "ruby"], ["lang", "en"]]) #=> "q=ruby&lang=en" URI.encode_www_form("q" => "ruby", "lang" => "en") #=> "q=ruby&lang=en" URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en") #=> "q=ruby&q=perl&lang=en" URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]]) #=> "q=ruby&q=perl&lang=en"
Upvotes: 1