1

I'm trying to get the request headers using vba. In other languages like python there is a very easy way to get that .headers. However, in case of vba I'm getting stuck. I know though there is a way in vba to get response headers by using .getAllResponseHeaders.

How can I get request headers using vba?

This is how I got response headers:

Sub GetRequestHeaders()
    Const URL$ = "https://stackoverflow.com/questions/tagged/web-scraping"
    Dim Http As New XMLHTTP60, S$

    With Http
        .Open "GET", URL, False
        .send
        S = .getAllResponseHeaders
        MsgBox S
    End With
End Sub
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
robots.txt
  • 96
  • 2
  • 10
  • 36
  • What your python code is printing out above is response headers. Where are you printing request headers? req is the response ... – QHarr Dec 26 '18 at 09:07
  • 1
    Right you are @QHarr. That is indeed response headers. However, what I wanted to achieve is get the cookies on the fly (instead of hardcoding) but `xmlhttp` request doesn't have that feature so I got success using `serverxmlhttp` request. – robots.txt Dec 26 '18 at 09:38
  • Indeed. That is a slightly different question and I would have referred you here: https://stackoverflow.com/a/39801506/6241235 . Nice answer that one! The answer to your above question is kinda as per below. – QHarr Dec 26 '18 at 09:41
  • Check out what I've posted out there in your linked post. Perhaps the easiest one @QHarr. – robots.txt Dec 26 '18 at 09:54

2 Answers2

4

Python .headers (assuming using requests library) provides the response headers, not the request headers. You add custom request headers in the dict passed with the get. Just as you have seen you can read response headers in vba with getAllResponseHeaders and send custom headers with setRequestHeader in key,value pairs.

I would normally use something like Fiddler/Dev tools network tab/Wireshark to see what request headers are sent then mimic those or read the associated API guidance specifying what custom request headers are expected.

Here is a snapshot of Fiddler request header capture for that url

enter image description here

There might be a way with WinHTTP using winhttpqueryheaders but haven't researched enough and it is far simpler to just use the network tab in most cases.

There is no method associated with the xmlhttp request/response to get the request headers. Have a look at the specification (WHATWG living standard) for associated methods (may be some variation in VBA implementation):

To deal with cookies look at MSXML2.ServerXMLHTTP. Example.

QHarr
  • 83,427
  • 12
  • 54
  • 101
0

You should set your request headers programmatically with a .setRequestHeader() method or it'll be default IE settings. Why do you need to get back the data you've already got?

  • Is it supposed to be an answer `why do i need this` @Irregular Expression? Btw, where did i ask how to set request headers? My question is how can I get request headers? Thanks. – robots.txt Dec 26 '18 at 08:43
  • There's no API in XMLHTTP to get *request* headers. You can log/monitor your networking to check for **unexpected** headers in your requests, also you can (and should) set all **expected** headers by yourself with setRequestHeaders(). That's all. – Irregular Expression Dec 26 '18 at 09:23
  • https://stackoverflow.com/questions/37120655/how-do-i-get-the-request-headers-of-a-winhttp-request?rq=1 - maybe, this'll be helpful. – Irregular Expression Dec 26 '18 at 09:31