Response 400 from Google Vision API OCR with a base64 string of specified image

不问归期 提交于 2019-12-06 11:42:05

The error, "message": "Invalid JSON payload received. Unexpected token.\nrequests=image&reque\n^", states you are passing a non-json format which is required to be a json. So, you should convert it to json and pass it to the request, as shown below.

response = requests.post(
url='https://vision.googleapis.com/v1/images:annotate?key={}'.format(API_KEY),
# import json module
# dumps the object to JSON
data=json.dumps(sending_request), 
headers={'Content-Type': 'application/json'}

It will trigger typeError: Object of type 'bytes' is not JSON serializable at the line of json.dumps([sending_request]) because you are not decoding the b64encode image. So, do this first and send the request

content = base64.b64encode(image).decode('UTF-8')

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!