Python 3.x 谷歌云功能没有返回响应
我在谷歌云函数Python 3.x 谷歌云功能没有返回响应,python-3.x,curl,flask,google-cloud-platform,google-cloud-functions,Python 3.x,Curl,Flask,Google Cloud Platform,Google Cloud Functions,我在谷歌云函数https://ocr.space/ocrapi 这是我部署的功能 def ocr_space_url(request): request_json = request.get_json() request_args = request.args if request_json and 'url' in request_json: url = request_json['url'] elif request_args and '
https://ocr.space/ocrapi
这是我部署的功能def ocr_space_url(request):
request_json = request.get_json()
request_args = request.args
if request_json and 'url' in request_json:
url = request_json['url']
elif request_args and 'url' in request_args:
url = request_args['url']
else:
url = 'http://www.africau.edu/images/default/sample.pdf'
headers = {
'apikey': 'helloworld',
}
payload = {'url': url,
}
r = requests.post('https://api.ocr.space/parse/image',
headers=headers, data=payload,
)
return r.content.decode()
这样部署:
gcloud函数部署ocr\u space\u url——运行时Python 37——触发http
调用方法:
curl-X POST”https://us-central1-prefab-environs-241910.cloudfunctions.net/ocr_space_url“-H”内容类型:application/json“-d”{“url”:”http://dl.a9t9.com/ocrbenchmark/pdfscan.pdf“}”
当我使用内容类型调用它时,它会给出以下错误
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
400错误请求
错误的请求
浏览器(或代理)发送了此服务器无法理解的请求
如果我在没有内容类型的情况下调用它,它会进入else,因为我得到了request\u json=None
,对于else中的url,它会给出正确的结果
这是我有生以来第一次使用云函数,请帮助我使用request作为参数,将POST数据解析为json(或任何你想要的)
使用--data'{“url”发出请求:http://www.africau.edu/images/defaultsample.pdf“}'-H”内容类型:application/json
现在我收到了这个错误错误:无法处理请求
您是否将请求
放入requirements.txt中?问题解决了。新问题TypeError:“Request”对象不可编辑
我在Request\u json=Request.get\u json()
@irumzahra使用”
而不是:-d'{“url”:http://dl.a9t9.com/ocrbenchmark/pdfscan.pdf“}”
#def ocr_space_url(url, overlay=False, api_key='helloworld', language='eng'):
def ocr_space_url(request):
""" OCR.space API request with remote file.
Python3.5 - not tested on 2.7
:param url: Image url.
:param overlay: Is OCR.space overlay required in your response.
Defaults to False.
:param api_key: OCR.space API key.
Defaults to 'helloworld'.
:param language: Language code to be used in OCR.
List of available language codes can be found on https://ocr.space/OCRAPI
Defaults to 'en'.
:return: Result in JSON format.
"""
request_json = request.get_json()
if request_json and 'url' in request_json:
url = request_json['url']
else:
url = 'http://www.africau.edu/images/defaultsample.pdf'
payload = {'url': url,
'isOverlayRequired': False,
'apikey': 'helloworld',
'language': 'eng',
}
r = requests.post('https://api.ocr.space/parse/image',
data=payload,
)
return r.content.decode()