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())
adboco
  • 2,840
  • 1
  • 21
  • 21
ashwini prakash
  • 135
  • 1
  • 12
  • 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')` – Sven-Eric Krüger Dec 20 '18 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: ...` – Sven-Eric Krüger Dec 20 '18 at 10:35

1 Answers1

1

[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())
ashwini prakash
  • 135
  • 1
  • 12