Skip to content

Instantly share code, notes, and snippets.

@hutch
Created December 18, 2009 00:15
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 hutch/259148 to your computer and use it in GitHub Desktop.
Save hutch/259148 to your computer and use it in GitHub Desktop.
Code to demonstrate problems with some Ruby HTTP clients when dealing with multi-value params
require 'rubygems'
require 'patron'
require 'typhoeus'
def req_typhoeus(several=nil)
if several then
response = Typhoeus::Request.post("http://localhost:4567/", params: { 'several' => several } )
else
response = Typhoeus::Request.post("http://localhost:4567/")
end
puts "typhoeus :: status: #{ response.code }, body: #{ response.body }"
end
def req_patron(several=nil)
request = Patron::Session.new
if several then
response = request.post('http://localhost:4567/', { 'several' => several })
else
response = request.post('http://localhost:4567/', { })
end
puts "patron :: status: #{ response.status }, body: #{ response.body }"
end
def req(several=nil)
req_patron(several)
req_typhoeus(several)
end
req
req('hello')
req(%w{ one two three })
require 'rubygems'
require 'sinatra'
#require 'rack-monkey-patch'
post '/' do
puts "#{File.basename(__FILE__)}:#{__LINE__} #{ __method__ } params: #{ params.inspect }"
several = params[:several]
puts "several(#{ several.class.name }) --> #{ several.inspect }"
case several
when String
"String --> #{ several }"
when Array
"Array[#{ several.size }] --> #{ several.join('-') }"
else
"eh?"
end
end
# running the client against the monkey patched rack server
patron :: status: 200, body: eh?
typhoeus :: status: 200, body: eh?
patron :: status: 200, body: String --> hello
typhoeus :: status: 200, body: String --> hello
patron :: status: 200, body: String --> ["one", "two", "three"]
typhoeus :: status: 200, body: Array[3] --> one-two-three
# running the client against the standard rack server
patron :: status: 200, body: eh?
typhoeus :: status: 200, body: eh?
patron :: status: 200, body: String --> hello
typhoeus :: status: 200, body: String --> hello
patron :: status: 200, body: String --> ["one", "two", "three"]
typhoeus :: status: 200, body: String --> three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment