Ruby: Can net / http make GET and POST requests at the same time?

Is it possible to pass both GET and POST parameters at the same time?

uri = URI.parse("http://www.example.com/post.php?a=1&b=2") req = Net::HTTP::Post.new(uri.path, { 'Referer' => "http://www.example.com/referer", 'User-Agent'=> "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)", 'Cookie' => $cookie }) req.set_form_data({ 'foo' => 'bar', 'bar' => 'foo' }) http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = 40 http.read_timeout = 20 # Request page: begin resp = http.request(req) rescue Exception puts "Exception requesting the page; returning" end 

Only the POST parameters are passed to the script above and the GET request is ignored

+6
ruby uri
source share
1 answer

When creating a request, you just need to save the GET parameters in the path:

 req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}", { 'Referer' => "http://www.example.com/referer", 'User-Agent'=> "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)", 'Cookie' => $cookie }) 

Please note that instead of uri.path am I adding to it ? and uri.query . This should pass GET as well as POST parameters.

+4
source

All Articles