Python POST request error "image format unsupported" using Microsoft Face API
<p>I'm trying to send a binary image file to test the Microsoft Face API. Using POSTMAN works perfectly and I get back a <code>faceId</code> as expected. However, I try to transition that to Python code and it's currently giving me this error: </p>

<pre><code>{"error": {"code": "InvalidImage", "message": "Decoding error, image format unsupported."}}
</code></pre>

<p>I read this <a href="http://stackoverflow.com/questions/36120746/decoding-error-image-format-unsupported-in-microsoft-face-api">SO post</a> but it doesn't help. Here's my code for sending requests. I'm trying to mimic what POSTMAN is doing such as labeling it with the header <code>application/octet-stream</code> but it's not working. Any ideas?</p>

<pre><code>url = "https://api.projectoxford.ai/face/v1.0/detect"

headers = {
  'ocp-apim-subscription-key': "&lt;key&gt;",
  'content-type': "application/octet-stream",
  'cache-control': "no-cache",
}

data = open('IMG_0670.jpg', 'rb')
files = {'IMG_0670.jpg': ('IMG_0670.jpg', data, 'application/octet-stream')}

response = requests.post(url, headers=headers, files=files)

print(response.text)
</code></pre>

<p>So the API endpoint takes a byte array but also requires the input body param as <code>data</code>, not <code>files</code>. Anyway, this code below works for me. </p>

<pre><code>url = "https://api.projectoxford.ai/face/v1.0/detect"

headers = {
  'ocp-apim-subscription-key': "&lt;key&gt;",
  'Content-Type': "application/octet-stream",
  'cache-control': "no-cache",
}

data = open('IMG_0670.jpg', 'rb').read()

response = requests.post(url, headers=headers, data=data)

print(response.text)
</code></pre>

python azure python-requests azure-api-apps microsoft-cognitive