-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathheaders.rb
More file actions
27 lines (21 loc) · 720 Bytes
/
headers.rb
File metadata and controls
27 lines (21 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
require "net/http"
require "uri"
uri = URI.parse("http://google.com/")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request["User-Agent"] = "My Ruby Script"
request["Accept"] = "*/*"
response = http.request(request)
# Get specific header
response["content-type"]
# => "text/html; charset=UTF-8"
# Iterate all response headers.
response.each_header do |key, value|
p "#{key} => #{value}"
end
# => "location => http://www.google.com/"
# => "content-type => text/html; charset=UTF-8"
# ...
# Alternatively, reach into private APIs.
p response.instance_variable_get("@header")
# => {"location"=>["http://www.google.com/"], "content-type"=>["text/html; charset=UTF-8"], ...}