Skip to content

Instantly share code, notes, and snippets.

@assimovt
Created June 1, 2010 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save assimovt/421087 to your computer and use it in GitHub Desktop.
Save assimovt/421087 to your computer and use it in GitHub Desktop.
# 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