将当前URL参数传递到IBM Cloud上的Cloud Function Python API

问题描述 投票:0回答:1

我是新手,正在尝试在IBM Cloud上创建Cloud Function。我的API仅在“ hello world”上运行良好。我需要从URL传递参数以将其操纵到我的Python API中。喜欢:

URL:https://fa75e0fa.eu-gb.apigw.appdomain.cloud/testapi/test1?id=11

我需要将上述URL末尾的id = 11的值传递到我的python代码(Python 3.70)中。

我现在有这个:

#
#
# main() will be run when you invoke this action
#
# @param Cloud Functions actions accept a single parameter, which must be a JSON object.
#
# @return The output of this action, which must be a JSON object.
#
#
import sys

def main(dict):
    return { 'message': 'Hello world' }

输出为:{“消息”:“你好世界”}

我尝试过:

import sys
import urllib3, requests, json
import requests
import os

def main(dict):
    id1=requests.GET.get('id')

    return { 'message': 'Hello world',
             'id': json.loads(id1.text)

    }

输出为:

激活ID:4e97b3be9b2b49f397b3be9b2b99f34d结果:{“错误”:“模块'请求'没有属性'GET'”}日志:

[“ 2020-04-16T11:29:34.215661Z stderr:追溯(最新最后调用):“,” 2020-04-16T11:29:34.215717Z stderr:文件\“ / action / 1 / src / exec __。py \”,第66行,“ 2020-04-16T11:29:34.215722Z stderr:res = main(payload)”,“ 2020-04-16T11:29:34.215725Z stderr:文件\“ / action / 1 / src / main __。py \”,第10行,位于main中,“ 2020-04-16T11:29:34.215728Z stderr:id1 = requests.GET.get('id')”,“ 2020-04-16T11:29:34.215731Z stderr:AttributeError:模块'requests'没有属性'GET'”,“ 2020-04-16T11:29:34.215734Zstderr:“]

您能帮忙吗?谢谢。

python api ibm-cloud ibm-cloud-functions
1个回答
0
投票
def main(args):
    arg1=args.get("arg1")
    myarg=args.get("anotherarg")

    return {"one": arg1, "two": myarg}

参数在您可以轻松访问的环境中传递。它们不是请求结构的一部分。 arg1将是您的id

import sys
import urllib3, requests, json
import requests
import os

def main(dict):
    id1=dict.get('id')

    return { 'message': 'Hello world',
             'id': id1)

    }
© www.soinside.com 2019 - 2024. All rights reserved.