Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Headers for Net::HTTP::Post in ruby

I have the following bit of code:

    uri = URI.parse("https://rs.xxx-travel.com/wbsapi/RequestListenerServlet")
    https = Net::HTTP.new(uri.host,uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.path)
    req.body = searchxml
    req["Accept-Encoding"] ='gzip'
    res = https.request(req)

This normally works fine but the server at the other side is complaining about something in my XML and the techies there need the xml message AND the headers that are being sent.

I've got the xml message, but I can't work out how to get at the Headers that are being sent with the above.

like image 981
nexar Avatar asked Mar 24 '11 09:03

nexar


2 Answers

To access headers use the each_header method:

# Header being sent (the request object):
req.each_header do |header_name, header_value|
  puts "#{header_name} : #{header_value}"
end

# Works with the response object as well:
res.each_header do |header_name, header_value|
  puts "#{header_name} : #{header_value}"
end
like image 154
Swanand Avatar answered Oct 18 '22 21:10

Swanand


you can add:

https.set_debug_output $stderr

before the request and you will see in console the real http request sent to the server.
very useful to debug this kind of scenarios.

like image 11
ALoR Avatar answered Oct 18 '22 20:10

ALoR