Get header values of reply using pycurl
Question: 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?


Answer: **If you have to use PyCurl**

Then you can pass a callback function to get the header information:

    
    
    # code...
    
    # Callback function invoked when header data is ready
    def header(buf):
        # Print header data to stderr
        import sys
        sys.stderr.write(buf)
        # Returning None implies that all bytes were written
    
    # more code...
    
    c.setopt(pycurl.HEADERFUNCTION, header)
    
    # yet more code...
    

Find out more from [the
docs](http://pycurl.sourceforge.net/doc/callbacks.html).

**You can also use requests instead of pycurl**

While this may not be possible, and does not directly answer your question, I
would recommend that you use the [requests
library](https://github.com/kennethreitz/requests) instead of pyCurl:

    
    
    import requests
    
    payload = {"key":"value"}
    cookies = {"key":"value"}
    
    r = requests.post('https://my.example.com', data=payload, cookies=cookies)
    
    location = r.headers["Location"]
    content  = r.text
    
    print(content)
    

It will make your life much easier. Find out more by [reading the
docs](http://www.python-requests.org/)

