1

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())
2
  • I would suggest to read about image uploads on the specific REST API you are sending requests to. It might be the case that you have to encode the image with base64... 'form': (base64.encodestring(fobj.read()), 'image/jpg') Commented Dec 20, 2018 at 10:33
  • I also would highly recommend to not open file object without closing it. Use a context manager instead... with open('filename.ext', 'rb') as fobj: ... Commented Dec 20, 2018 at 10:35

1 Answer 1

2

[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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.