Skip to content

Instantly share code, notes, and snippets.

@takeru
Created April 28, 2010 04:36
Show Gist options
  • Select an option

  • Save takeru/381740 to your computer and use it in GitHub Desktop.

Select an option

Save takeru/381740 to your computer and use it in GitHub Desktop.
# ADD support "req.body" for appengine-apis.
# http://www.ruby-lang.org/ja/man/html/Net_HTTPRequest.html
#######################################
## webserver.rb
require 'webrick'
s = WEBrick::HTTPServer.new(
:BindAddress => '127.0.0.1',
:Port => 18080,
:Logger => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG)
)
s.mount_proc('/') do |req, res|
s = "request_method=#{req.request_method} content_length=#{req.content_length} body=[#{req.body}] body.size=#{req.body.size rescue nil}"
puts s
res.body = s
res.content_length = s.size
res.content_type = "text/plain"
end
Signal.trap('INT'){ s.shutdown }
s.start
################################
## httppost.rb
require 'net/http'
Net::HTTP.version_1_2
Net::HTTP.start('localhost', 18080) {|http|
response = http.post('/test1', 'a=1&b=2')
puts response.body
}
http = Net::HTTP.new('localhost', 18080)
req = Net::HTTP::Post.new("/test2")
req.body = "c=3&d=4"
res = http.request(req)
puts res.body
######################################
=begin
tkrmb:~/rails/.../versions/hello-twit% ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin8]
tkrmb:~/rails/.../versions/hello-twit% ruby httppost.rb
request_method=POST content_length=7 body=[a=1&b=2] body.size=7
request_method=POST content_length=7 body=[c=3&d=4] body.size=7
tkrmb:~/rails/.../versions/hello-twit% jruby -v
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.6.0_03-p3) [i386-java]
tkrmb:~/rails/.../versions/hello-twit% jruby httppost.rb
request_method=POST content_length=7 body=[a=1&b=2] body.size=7
request_method=POST content_length=7 body=[c=3&d=4] body.size=7
# appengine-apis
tkrmb:~/rails/.../versions/hello-twit% LANG=C appcfg.rb run httppost.rb
request_method=POST content_length=7 body=[a=1&b=2] body.size=7
request_method=POST content_length=0 body=[] body.size=
appengine-apis is not supported "req.body"
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment