Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display HTTP headers using Open::URI?

with Open::URI, I can do the following:

require 'open-uri'
#check status
open('http://google.com').status
#get entire html
open('http://google.com').read

Is it possible to get the HTTP headers of a request so things can be debugged, something like Curls' curl -I http://google.com?

$ curl -I google.com
HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Mon, 17 Dec 2012 14:28:17 GMT
Expires: Wed, 16 Jan 2013 14:28:17 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

Is this possible?

like image 867
CuriousMind Avatar asked Dec 17 '12 14:12

CuriousMind


People also ask

How do I view HTTP headers?

To view the request or response HTTP headers in Google Chrome, take the following steps : In Chrome, visit a URL, right click , select Inspect to open the developer tools. Select Network tab. Reload the page, select any HTTP request on the left panel, and the HTTP headers will be displayed on the right panel.

Which HTTP request header is used to identify the URI of the resource?

Referer: This optional header field allows the client to specify, for the server's benefit, the address ( URI ) of the document (or element within the document) from which the URI in the request was obtained.

How do I use HTTP headers?

HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its case-insensitive name followed by a colon ( : ), then by its value. Whitespace before the value is ignored.


1 Answers

Use the meta method of the virtual filehandle:

open('http://google.com'){|f| pp f.meta  }
{"x-frame-options"=>"SAMEORIGIN",
 "expires"=>"-1",
 "p3p"=>
  "CP=\"This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.\"",
 "content-type"=>"text/html; charset=ISO-8859-1",
 "date"=>"Mon, 17 Dec 2012 14:37:29 GMT",
 "server"=>"gws",
 "x-xss-protection"=>"1; mode=block",
 "set-cookie"=>
  "PREF=ID=d2fb8a93d369bcd2:FF=0:TM=1355755049:LM=1355755049:S=ONVSP6n2jtluFgll; expires=Wed, 17-Dec-2014 14:37:29 GMT; path=/; domain=.google.com, NID=67=OFEvvHCOa3C6wScQCUIKfu_89oL9MSmnFjwN-u5LX_foP8NLsX7G9dq48NLVrf4WUXhqOA1jb38s0e9qeRp1Iwx_LT_N8IuF0Qi6dXVtR2zdvA86INqtfg5uNrKvxJfJ; expires=Tue, 18-Jun-2013 14:37:29 GMT; path=/; domain=.google.com; HttpOnly",
 "cache-control"=>"private, max-age=0",
 "transfer-encoding"=>"chunked"}

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI/Meta.html

like image 51
Unixmonkey Avatar answered Sep 30 '22 18:09

Unixmonkey