Search code examples
pythoncookieslibcurlpycurl

PyCurl 7.19.0: get cookies from response using getinfo(pycurl.COOKIELIST)


I'd like to get the Cookies that have been carried with the response (Set-Cookie: name=value; ...).

When passing the info constant and the reference of an empty list:

set_cookies = []
c.getinfo(c.COOKIELIST, set_cookies)

I get the following error:

TypeError: c.getinfo() takes exactly 1 argument (2 given)

That was straightforward.

I also tried this signature:

set_cookies = c.getinfo(c.COOKIELIST)

I get this error:

ValueError: invalid argument to getinfo

This is a bit vague, however.

Getting the HTTP status code using getinfo() works fine.

I get a 200 OK and Set-Cookie headers, of course (debugging with c.setopt(pycurl.VERBOSE, 1)).

Of course I've read

With very few exceptions, PycURL constants are derived from libcurl constants by removing the CURLINFO_ prefix.

Maybe this is the exception?

In my opinion the documentation is a quite poor and does not contain any info about getting cookies sent with Set-Cookie.

References:


Solution

  • There's no method to get response cookies after calling perform(). Headers sent by the server can be captured using a callback function passed to the PyCurl instance while configuring with setopt():

    CURLOPT_HEADERFUNCTION

       Callback for writing received headers. See CURLOPT_HEADERFUNCTION 
    

    Example code excerpt:

        set_cookies = []
    
        # closure to capture Set-Cookie
        def _write_header(header):
            match = re.match("^Set-Cookie: (.*)$", header)
    
            if match:
                set_cookies.append(match.group(1))
    
        # use closure to collect cookies sent from the server
        c.setopt(pycurl.HEADERFUNCTION, _write_header)
    

    References: