【问题标题】:Python Google Cloud Function call from Flutter. Response is not valid JSON object来自 Flutter 的 Python Google Cloud 函数调用。响应不是有效的 JSON 对象
【发布时间】:2020-08-17 01:55:07
【问题描述】:

我是 Google Cloud Platform 和 Flutter 的新手,我想从我的 Flutter 项目中调用一个函数。这个 GCF 计算两个字符串之间的 Levenshtein 距离,实现如下:

from flask import jsonify, abort
from Levenshtein import distance as levenshtein_distance, ratio, editops as string_ops_needed

def json_abort(status_code, message):
    data = {
        'error': {
            'code': status_code,
            'message': message
        }
    }
    response = jsonify(data)
    response.status_code = status_code
    abort(response)

def word_distance(request, decoded_token = None):

    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <http://flask.pocoo.org/docs/1.0/api/#flask.Request>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
    """
    request_json = request.get_json(silent=True)
    '''request_args = request.args'''

    if request_json and "candidate_string" in request_json and "correct_string" in request_json:
        candidate_string=request_json["candidate_string"]
        correct_string=request_json["correct_string"]

        distance=levenshtein_distance(candidate_string,correct_string)

        response = jsonify(distance=distance, status_code=200)
        return response
    else:
        json_abort(400, message="Missing params")

当我尝试从颤振中调用它时,颤振返回以下代码 发生异常。 PlatformException (PlatformException(functionsError, Cloud function failed with exception., {message: Response is not valid JSON object., details: null, code: INTERNAL}))

final HttpsCallable callable = CloudFunctions.instance
        .getHttpsCallable(functionName: 'word_distance')
          ..timeout = const Duration(seconds: 10);

    dynamic result = await callable.call(
      <String, dynamic>{
        "candidate_string": candidateString,
        "correct_string": correctString
      },
    );

当我在 GCF 中使用 {"candidate_string": "helo worlt", "correct_string": "hello world"} 测试函数时

返回结果{"distance":2,"status_code":200}

知道为什么会发生这种情况以及如何解决这个问题吗?我找到了这个post,但它没有显示如何进行的具体示例。

【问题讨论】:

  • 您是否能够通过手动curl-ing 函数的端点来获得正确的响应?您的函数是否允许未经身份验证的调用?您的函数的日志中有任何内容吗?

标签: python flutter google-cloud-functions


【解决方案1】:

好吧,您使用 Map 参数调用该函数,我认为这是导致错误的原因,因为它无法正确解析为 JSON

尝试使用

<String, dynamic>{
        '"candidate_string"': '"$candidateString"',
        '"correct_string"': '"$correctString"'
      }

我在通过TCP 协议发送 json 数据时多次遇到此错误,上面的那个对我有用,原因是它可以很容易地解析成 JSON

【讨论】:

  • 我尝试了这种方法,但它仍然检索到相同的错误。
【解决方案2】:

按照 Dustin Ingram 的建议查看 GCF 日志后,我发现我的函数正在被访问,但参数和输出不正常。所以在再次深入检查了这个example,并考虑到Google specs for https.onCall之后,我发现有必要返回一个'data'/'error' json。

我将 GCF 更改为以下内容,现在它返回了预期值。

from flask import jsonify, abort

from Levenshtein import distance as levenshtein_distance, ratio, editops as string_ops_needed

def word_distance(request):

    """HTTP Cloud Function.
    Args:
        request (flask.Request): The request object.
        <http://flask.pocoo.org/docs/1.0/api/#flask.Request>
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
    """

    request_json = request.json.get('data')

    if "candidate_string" in request_json and "correct_string" in request_json:
        candidate_string=request_json["candidate_string"]
        correct_string=request_json["correct_string"]

        distance=levenshtein_distance(candidate_string,correct_string)

        return jsonify({
            'data': {
                'distance': distance
            }
        })
    else:
        data = {
            'error': {
                'code': 400,
                'message': 'Missing params'
            }
        }
        response = jsonify(data)
        response.status_code = 400
        return(response)

【讨论】:

    猜你喜欢
    • 2021-06-24
    • 2021-01-21
    • 2018-12-13
    • 2020-07-26
    • 2019-08-25
    • 2015-02-14
    • 2022-07-07
    • 2021-03-03
    • 2019-03-03
    相关资源
    最近更新 更多