简体   繁体   中英

How to convert a cURL request to Python request

I have a cURL request that is working fine.

curl http://localhost:5000/models/images/generic/infer.json -XPOST -F job_id='123' -F dont_resize='dont_resize' -F snapshot_epoch='100' -F image_file='@/home/hellouser/Downloads/infer/Users/User01/Images/tiles/999/00.jpg'`

I have a python script where I want to execute the same request. But I get the following error,

{ "error": { "message": "'NoneType' object has no attribute 'iteritems'", "type": "AttributeError" } }

Here is the python code,

import requests
data = {
    'job_id': '123',
    'dont_resize': 'dont_resize',
    'snapshot_epoch': '100',
    'image_file': '@/home/hellouser/Downloads/infer/Users/User01/Images/tiles/999/00.jpg'    }

url = 'http://localhost:5000/models/images/generic/infer.json'
r = requests.post(url=url, data=data)

Any idea how to properly convert the code? Should I pass file=file in request?

Try this:

data = {
    'job_id': '123',
    'dont_resize': 'dont_resize',
    'snapshot_epoch': '100',
}
files = {
    'image_file': open('/home/hellouser/Downloads/infer/Users/User01/Images/tiles/999/00.jpg', 'rb')
}

url = 'http://localhost:5000/models/images/generic/infer.json'
r = requests.post(url=url, data=data, files=files)

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