Search code examples
pythonpycurl

How to add Pycurl headers


I can't understand this works How to add multiple headers to pycurl HTTPS request, I tried to do it but it accepts the user agent only and gives me an error if I tried to add more headers

url = "https://blabla.com"
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.CAINFO, certifi.where())
c.setopt(c.URL, url)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.HTTPHEADER, custom_headers)
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.FOLLOWLOCATION, True)
c.perform()
c.close()
body = buffer.getvalue()
response = body.decode("utf-8")
print (response)

Solution

  • Do you have to set the headers as a list

    custom_headers = ['example1: ex1', 'example2: ex2']
    curl.setopt(pycurl.HTTPHEADER, custom_headers)
    

    From the pycurl documentation at http://pycurl.io/docs/latest/curlobject.html?highlight=httpheader

    HTTP200ALIASES, HTTPHEADER, POSTQUOTE, PREQUOTE, PROXYHEADER and QUOTE accept a list or tuple of strings. The same rules apply to these strings as do to string option values. Example:

    c.setopt(pycurl.HTTPHEADER, ["Accept:"])
    c.setopt(pycurl.HTTPHEADER, ("Accept:",))