[英]Pass current URL parameters to Cloud Function Python API on IBM Cloud
我是新手,正在尝试在 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' }
output 是: { "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)
}
output 是:
激活 ID:4e97b3be9b2b49f397b3be9b2b99f34d 结果:{“错误”:“模块 'requests' 没有属性 'GET'”} 日志:
[ "2020-04-16T11:29:34.215661Z stderr: Traceback (最近一次通话最后一次):", "2020-04-16T11:29:34.215717Z stderr: File \"/action/1/src/exec__.py \",第 66 行,在 ",
“2020-04-16T11:29:34.215722Z 标准错误:res = main(有效负载)”,
“2020-04-16T11:29:34.215725Z 标准错误:文件 \"/action/1/src/main__.py\",第 10 行,在 main",
“2020-04-16T11:29:34.215728Z 标准错误:id1=requests.GET.get('id')”,
"2020-04-16T11:29:34.215731Z stderr: AttributeError: 模块 'requests' 没有属性 'GET'", "2020-04-16T11:29:34.215734Z
标准错误:“]
你能帮忙吗? 谢谢。
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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.