简体   繁体   中英

how to convert curl example to Pycurl

PS - No I am not going to use python requests, cause it does not help creating readable streams of the file.

curl -i -H "Authorization":"eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY" \
    -F user_data='{"user data": {"preferred_city":"Newyork","within_radious":"5"}}' \
    -F uploaded_documents=@mydocument.pdf \
    http://127.0.0.1:5000/api/city

I am trying to send both file and a json data in pycurl post method.
I am succesful in sending file alone only.
I found above example to send file and data in same api but not able to convert it to pycurl systax properly.
Can anyone please convert it to pycurl type code work?

This will work for you

import pycurl
c = pycurl.Curl()      
c.setopt(pycurl.URL, "http://127.0.0.1:5000/api/city")
c.setopt(c.POST, 1)
c.setopt(pycurl.HTTPHEADER, ['Authorization: "eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY"'])
data = json.dumps({"user data": {"preferred_city":"Newyork","within_radious":"5"}})
c.setopt(pycurl.POSTFIELDS,data)
c.setopt(c.HTTPPOST, [("uploaded_documents", (c.FORM_FILE, "mydocument.pdf"))])
c.perform()
c.close()

You can try this package - http_pycurl .

The API it exposes is similar to the requests.

How to install

pip install http_pycurl

Specific usage is here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM