ashwini prakash
ashwini prakash

Reputation: 145

Python Rest API POST an image

Below is my code. I am trying to make a POST operation using python with REST API. I have an image I want to post. I get error saying;

"'code': 'BadRequest', 'message': "Could not process incoming request: 'Missing content-type boundary.'. Please ensure that it is well-formed"

Where am I making mistake?

import requests
headers = {
    'accept': 'application/json',
    'Content-Type': 'multipart/form-data',
    #'boundary':'---BOUNDRY'
}
params = (
    ('returnFaceId', 'true'),
    ('returnFaceLandmarks', 'true'),
)
files = {
    'form': (open('image.jpg', 'rb'),'image/jpg'),
}
response = requests.post('http://localhost:5000/face/v1.0/detect', headers=headers, params=params, files=files)
print (response.json())

Upvotes: 1

Views: 9417

Answers (1)

ashwini prakash
ashwini prakash

Reputation: 145

[multipart data POST using python requests: no multipart boundary was found

Above link was helpful. I removed explicit header and Parameters, and it worked.

import requests

files = {
    'form': ('images.jpg',open('images.jpg', 'rb'),'image/jpg'),
}

response = requests.post('http://localhost:5000/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false', files=files)
print(response.json())

Upvotes: 2

Related Questions