Code to accompany the weblog posting: Ruby/Rack and Multiple Value Request Param Pain — Part Two
Demonstrates problems with some Ruby HTTP clients when dealing with multi-value params
Code to accompany the weblog posting: Ruby/Rack and Multiple Value Request Param Pain — Part Two
Demonstrates 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 |