BlackHatSamurai BlackHatSamurai - 7 months ago 21
Ruby Question

Get response headers from Ruby HTTP request

I'm making an HTTP request with Ruby using Net::HTTP, and I can't figure out how to get all the response headers.

I tried

response.header
and
response.headers
and nothing is working.

Answer

The response object actually contains the headers.

See "Net::HTTPResponse" for more infomation.

You can do:

response['Cache-Control']

You can also call each_header or each on the response object to iterate through the headers.

If you really want the headers outside of the response object, call response.to_hash