Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get response header

Tags:

r

rcurl

httr

I would like to get response headers from GET or POST.

My example is:

    library(httr)
    library(RCurl)
    url<-'http://www.omegahat.org/RCurl/philosophy.html'
    doc<-GET(url)
    names(doc)

[1] "url"         "handle"      "status_code" "headers"     "cookies"     "content"     "times"       "config"  

but there is no response headers, only request headers.

Result shoud be something like this:

Connection:Keep-Alive
Date:Mon, 11 Feb 2013 20:21:56 GMT
ETag:"126a001-e33d-4c12cf2702440"
Keep-Alive:timeout=15, max=100
Server:Apache/2.2.14 (Ubuntu)
Vary:Accept-Encoding

Can I do this with R and httr/RCurl packages or R is not enough for this kind of problem?

Edit: I would like to get all response headers. I am mainly interested in Location response which is not in this example.

Edit2: I forgot to tell the system which I work on - it is Windows 7

My session.info

> sessionInfo()
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Polish_Poland.1250  LC_CTYPE=Polish_Poland.1250    LC_MONETARY=Polish_Poland.1250
[4] LC_NUMERIC=C                   LC_TIME=Polish_Poland.1250    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rjson_0.2.12 RCurl_1.95-3 bitops_1.0-5 httr_0.2     XML_3.95-0.1

loaded via a namespace (and not attached):
[1] digest_0.6.2  stringr_0.6.2 tools_2.15.2 
like image 627
Maciej Avatar asked Feb 11 '13 20:02

Maciej


People also ask

How do I get the HTTP response header?

In order to extract a header from a response, you will have to use Http. request along with the expectStringResponse function, which includes the full response including headers.

How do I get response headers in Fetch?

We then fetch this request using fetch() , extract a blob from the response using Response. blob , create an object URL out of it using URL. createObjectURL , and display this in an <img> . Note that at the top of the fetch() block, we log the response headers to the console.

What is a get request header?

A request header is an HTTP header that can be used in an HTTP request to provide information about the request context, so that the server can tailor the response. For example, the Accept-* headers indicate the allowed and preferred formats of the response.

DOES GET request have header?

Yes, you can send any HTTP headers with your GET request. For example, you can send user authentication data in the Authorization header, send browser cookies in the Cookie header, or even send some additional details about your request in custom headers like X-Powered-By or X-User-IP.


1 Answers

You can do it this way :

h <- basicHeaderGatherer()
doc <- getURI("http://www.omegahat.org/RCurl/index.html", headerfunction = h$update)
h$value()

Which will give you a named vector :

                            Date                           Server 
 "Mon, 11 Feb 2013 20:41:58 GMT"         "Apache/2.2.14 (Ubuntu)" 
                   Last-Modified                             ETag 
 "Wed, 24 Oct 2012 15:49:35 GMT" "\"3262089-10bf-4ccd0088461c0\"" 
                   Accept-Ranges                   Content-Length 
                         "bytes"                           "4287" 
                            Vary                     Content-Type 
               "Accept-Encoding"                      "text/html" 
                          status                    statusMessage 
                           "200"                             "OK" 
like image 69
juba Avatar answered Sep 17 '22 06:09

juba