Search code examples
pythonpycurlverbose

Get header values of reply using pycurl


I would like to know some ways to capture and access the header information of the reply when making a request with PyCurl:

c = pycurl.Curl() 
c.setopt(c.URL,'MY_URL')
c.setopt(c.COOKIEFILE,'cookies')
c.setopt(c.COOKIE,'cookies')
c.setopt(c.POST,1)
c.setopt(c.POSTFIELDS,'MY AUTH VALUES')
c.setopt(c.VERBOSE, True)
b = StringIO.StringIO()
c.setopt(c.WRITEFUNCTION, b.write)
c.perform()

The reply will be well-formatted JSON written to buffer b.

I wish to recover the value of the "Location" header in the reply.

When trying to use curl, this value can be seen in the verbose output:

[... Curl output ...]
> GET XXXXXXXXX
[... Request ...]
[... Curl output ...]
< HTTP/1.1 302 Found
[... Other headers ...]
< Location: YYYYYYYYYYYYYYY
[... Rest of reply ...]

How do I recover the value of the Location header from python?


Solution

  • Essentially, a lot of custom functions and registering callback functions. Let's go through curl's verbose output piecewise. First, the bit about the connection can mostly be filled in if you provide your own CURLOPT_OPENSOCKETFUNCTION.

    Next, the request headers can are things that you know ahead of time and can print out however you like. For the progress bar, there's CURLOPT_PROGRESSFUNCTION, which allows you to register a callback for updates to the progress "roughly once per second."

    You can also register a response header write function (CURLOPT_HEADERFUNCTION) which you can then use to capture and/or display the response headers.

    Alternatively, you can use CURLOPT_DEBUGFUNCTION to register callbacks to get information for the headers you send out, get in response, etc.