Gabriel
Gabriel

Reputation: 425

http post request to API with azure blob storage

I'm trying to make an http post request with Microsoft's face api, in order to connect it with photos in my azure blob storage account. When I run the following code, I get multiple errors like handshake error, or ssl routines type errors. I appreciate any help! The problem code is :

api_response = requests.post(url, headers=headers, data=blob)

obviously for context here is what I ran before that. This first chunk sets up the storage account:

%matplotlib inline
import matplotlib.pyplot as plt
import io
from io import StringIO
import numpy as np
import cv2
from PIL import Image  
from PIL import Image
import os 
from array import array


azure_storage_account_name = 'musicsurveyphotostorage'
azure_storage_account_key = None  #dont need key... we will access public blob... 
if azure_storage_account_name is None:
raise Exception("You must provide a name for an Azure Storage account")   
from azure.storage.blob import BlockBlobService
blob_service = BlockBlobService(azure_storage_account_name, azure_storage_account_key)

# select container (folder) name where the files resides
container_name = 'musicsurveyphotostorage'

# list files in the selected folder
generator = blob_service.list_blobs(container_name)
blob_prefix = 'https://{0}.blob.core.windows.net/{1}/{2}'

# load image file to process
blob_name = 'shiba.jpg'  #name of image I have stored
blob = blob_service.get_blob_to_bytes(container_name, blob_name)
image_file_in_mem = io.BytesIO(blob.content)
img_bytes = Image.open(image_file_in_mem)

This second chunk calls out the API and the problematic post request:

#CALL OUT THE API
import requests
import urllib

url_face_api = 'https://eastus.api.cognitive.microsoft.com/face/v1.0'
api_key ='____'

#WHICH PARAMETERS ATTRIBUTES DO YOU WANT RETURNED   
headers = {'Content-Type': 'application/octet-stream', 'Ocp-Apim- 
Subscription-Key':api_key}

params = urllib.parse.urlencode({
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'true',
    'returnFaceAttributes': 'age,gender,smile,facialHair,headPose,glasses',
})

query_string = '?{0}'.format(params)
url = url_face_api  + query_string


#THIS IS THE PROBLEM CODE  
api_response = requests.post(url, headers=headers, data=blob)
#print out output in json
import json
res_json = json.loads(api_response.content.decode('utf-8'))
print(json.dumps(res_json, indent=2, sort_keys=True))

Upvotes: 2

Views: 2262

Answers (1)

Tom Sun
Tom Sun

Reputation: 24569

If I open the Fiddler, I also could reproduce the issue that you mentioned. If it is that case, you could pause to capture the request with fiddler during send request.

Based on my test, in your code there are 2 code lines need to be changed. From more information you could refer to the screenshot. We also could get the some demo code from azure offical document.

url_face_api = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' # in your case miss detect
api_response = requests.post(url, headers=headers,data=blob.content) # data should be blob.content

enter image description here

Upvotes: 1

Related Questions