【问题标题】:How to fix "error": "'parameter_name'" when using IBM Cloud Function's REST API?使用 IBM Cloud Function 的 REST API 时如何修复“错误”:“'parameter_name'”?
【发布时间】:2019-10-30 03:27:59
【问题描述】:

我在 IBM Cloud Functions 中有一个仅接收一个参数的操作:“frame”。我正在使用 Postman 测试随操作提供的 REST API 端点。但是,当我提供“框架”参数时,它会返回以下内容:

"response": {
        "result": {
            "error": "'frame'"
        },
        "status": "application error",
        "success": false
    }

我在 IBM Cloud Functions 控制台中调用此操作时遇到了此问题。我通过擦除输入模式中的空格并再次添加它来解决它,然后它就像控制台中的魅力一样工作。但是,我不能对 HTTP 请求做同样的事情。

我目前做HTTP请求的方式是这样的:

POST https://us-south.functions.cloud.ibm.com/api/v1/namespaces/{namespace}/actions/{action_name}?blocking=true&frame={value}

该操作应该返回我期望的结果,但它现在没有这样做。请帮助我,任何答案都会很棒!

编辑:

这是动作的代码:

import requests, base64, json, cv2
from PIL import Image
from six import BytesIO

def json_to_dict(json_str):
    return json.loads(json.dumps(json_str))

def frame_to_bytes(frame):
    frame_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    pil_im = Image.fromarray(frame_im)
    stream = BytesIO()
    pil_im.save(stream, format="JPEG")
    stream.seek(0)
    img_for_post = stream.read()
    img_base64 = base64.b64encode(img_for_post)
    return img_base64

def main(dict):
    cap = cv2.VideoCapture(dict['frame'])
    if not cap.isOpened():
        return { "error": "Unable to open video source" }
    ret, frame = cap.read()
    if ret is False:
        return { "error": "Unable to read video source" }

    # openALPR API part
    OPENALPR_SECRET_KEY = {my_secret_key}
    url = "https://api.openalpr.com/v2/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s" % (
        OPENALPR_SECRET_KEY)
    r = requests.post(url, data=frame_to_bytes(frame))
    resp = json_to_dict(r.json())
    print(resp)
    if not resp['results']:
        return { "error": "Plate number not recognized" }
    plates = []
    for plate in resp['results']:
        if plate['confidence'] < 75:
            pass
        else:
            print(plate['plate'])
            plates.append(plate['plate'])
    return { "plates": plates  }

这是激活响应(根据 Postman 返回的状态是 502 Bad Gateway):

{
    "activationId": "5a83396b9f53447483396b9f53e47452",
    "annotations": [
        {
            "key": "path",
            "value": "{namespace}/{name}"
        },
        {
            "key": "waitTime",
            "value": 5531
        },
        {
            "key": "kind",
            "value": "python:3.7"
        },
        {
            "key": "timeout",
            "value": false
        },
        {
            "key": "limits",
            "value": {
                "concurrency": 1,
                "logs": 10,
                "memory": 1024,
                "timeout": 60000
            }
        },
        {
            "key": "initTime",
            "value": 3226
        }
    ],
    "duration": 3596,
    "end": 1560669652454,
    "logs": [],
    "name": "{name}",
    "namespace": "{namesapce}",
    "publish": false,
    "response": {
        "result": {
            "error": "'frame'"
        },
        "status": "application error",
        "success": false
    },
    "start": 1560669648858,
    "subject": "{my_email}",
    "version": "0.0.7"
}

编辑 2: 我还尝试将其作为 Web 操作启用,以查看它是否会改变任何内容。然而,这并没有什么用。当我使用这个 HTTP 请求时:

https://us-south.functions.cloud.ibm.com/api/v1/web/{namespace}/default/{action_name}?frame={value}

我明白了:

{
    "code": "e1c36666f4db1884c48f028ef58243fc",
    "error": "Response is not valid 'message/http'."
}

这是可以理解的,因为我的函数返回的是 json。但是,当我使用 this HTTP 请求时:

https://us-south.functions.cloud.ibm.com/api/v1/web/{namespace}/default/{action_name}.json?frame={value}

我明白了:

{
    "code": "010fc0efaa29f96b47f92735ff763f50",
    "error": "Response is not valid 'application/json'."
}

我真的不知道在这里做什么

【问题讨论】:

  • 请添加您的代码。激活记录中有什么?它应该显示一条错误消息
  • @data_henrik 添加了代码和激活记录。我假设激活记录是端点返回的响应。如果不是,请告诉我在哪里可以找到它,以便我发布它。
  • 您正在使用 frame 值的查询参数调用操作,但这仅支持 webaction。 Webaction 为操作 POST https://us-south.functions.cloud.ibm.com/api/v1/web/{namespace}/default/{action_name}?frame={value} 使用不同的 url
  • @user6062970 请参阅编辑 2
  • 在 URI 中的操作名称后添加 .json

标签: python-3.x ibm-cloud postman openwhisk ibm-cloud-functions


【解决方案1】:

在谷歌上搜索了一下后,我发现了一些现在对我有用的东西,尽管它可能并不适用于所有人。 Apache 有一个 python "client" example 用于使用使用 requests 库的操作的 REST API。

问题是,为了使用它,您需要提供您的 API KEY,除了直接从 IBM Cloud CLI 获取之外,我不知道如何通过任何其他方式获取它。由于我正在尝试从 Web 服务器访问该功能,因此我需要将密钥保存在环境变量中或将其保存在文本文件中并从那里访问它或在服务器上安装 CLI,使用我的凭据登录并拨打ibmcloud wsk property get --auth

此外,当我尝试此方法时,它不适用于 Web 操作端点。

【讨论】:

    猜你喜欢
    • 2021-01-17
    • 2017-07-17
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 2020-09-18
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多