Reputation: 53
Currently working on a project and I need to post this request:
curl -X POST "http://localhost:8000/api/v1/recognition/recognize" \
-H "Content-Type: multipart/form-data" \
-H "x-api-key: xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxx" \
-F "file=@<image>.jpg" #image
Tried several times with this code snippet had no luck:
import requests
headers = {
'Content-Type': 'multipart/form-data',
'x-api-key': 'xxxxxxxxxxxx',
}
files = {
'file': ('image.jpg', open('image.jpg', 'rb')),
}
response = requests.post('http://localhost:8000/api/v1/recognition/recognize', headers=headers, files=files)
print(response)
Am I doing something wrong or missing something?
Upvotes: 3
Views: 30446
Reputation: 53
I made it work with this code:
import requests
url = "http://10.0.38.119:8000/api/v1/recognition/recognize"
payload = {}
files = [('file', ('<image>.jpg', open('<image>.jpg', 'rb'), 'image/jpeg'))]
headers = {
'x-api-key': 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
Upvotes: 2
Reputation: 15689
The -H
flag means that you're passing a header, not a file.
>>> import requests
>>>
>>> headers = {
... "Content-Type": "multipart/form-data",
... "x-api-key": "xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxx"
... }
>>>
>>> file = {"file": ("<image>.jpg", open("<image>.jpg", "rb"))}
>>>
>>> r = requests.post("http://localhost:8000/api/v1/recognition/recognize",
... headers=headers, files=file
... )
Upvotes: 0
Reputation: 14218
try with this
import requests
headers = {
'Content-Type': 'multipart/form-data',
'x-api-key': 'xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxx',
}
files = {
'file': ('<image>.jpg', open('<image>.jpg', 'rb')),
}
response = requests.post('http://localhost:8000/api/v1/recognition/recognize', headers=headers, files=files)
Upvotes: 4