【问题标题】:Google Cloud Functions is not returning responseGoogle Cloud Functions 未返回响应
【发布时间】:2019-10-13 18:38:20
【问题描述】:

我在谷歌云函数https://ocr.space/ocrapi上使用这个api 这是我部署的功能

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 functions deploy ocr_space_url --runtime python37 --trigger-http

通过以下方式调用它:
curl -X POST "https://us-central1-prefab-environs-241910.cloudfunctions.net/ocr_space_url" -H "Content-Type: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>


如果我在没有内容类型的情况下调用它,它会进入 else,因为我得到 request_json = None 而对于 else 中的 url,它会给我正确的结果

我是第一次使用云功能,请帮忙

【问题讨论】:

    标签: python-3.x curl flask google-cloud-platform google-cloud-functions


    【解决方案1】:

    使用请求作为参数,将你的 POST 数据解析为 json(或任何你想要的)

    #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()
    

    使用--data '{"url": "http://www.africau.edu/images/defaultsample.pdf"}' -H "Content-Type: application/json" 提出请求

    【讨论】:

    • 现在我收到此错误Error: could not handle the request
    • 你把requests放到requirements.txt里了吗?
    • 问题解决了。新问题TypeError: 'Request' object is not iterable
    • 我在 request_json = request.get_json() 中没有得到任何信息
    • @irumzahra 使用' 而不是": -d '{"url": "http://dl.a9t9.com/ocrbenchmark/pdfscan.pdf"}'
    猜你喜欢
    • 2021-07-22
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多