Created
June 1, 2010 15:57
-
-
Save assimovt/421087 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using custom set_form_data and urlencode | |
# Ruby NET/HTTP does not support duplicate parameter names | |
# File net/http.rb, line 1426 | |
def set_form_data(request, params, sep = '&') | |
request.body = params.map {|k,v| | |
if v.instance_of?(Array) | |
v.map {|e| "#{urlencode(k.to_s)}=#{urlencode(e.to_s)}"}.join(sep) | |
else | |
"#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" | |
end | |
}.join(sep) | |
request.content_type = 'application/x-www-form-urlencoded' | |
end | |
def urlencode(str) | |
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment