我是新手,正在尝试在IBMCloud上创建云函数。我的API只在"helloworld"上运行良好。我需要将URL中的参数传递到我的Python API中进行操作。类似:
网址: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:4e97b3be9b2b49f397b3be9b2 b99f34d结果:{"error":"模块'requests'没有属性'GET'"}日志:
["2020-04-16T11:23.4215661Z标准错误:追溯(最新call last(:","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-16T1:29:34.215725Z stderr:File\"/action/1/src/main__.py\",第10行,在main中",
"2020-04-16T11:29:34.215728Z stderr:id1=请求.GET.GET('id'(",
"2020:04-16T1:29:34.215731Z stderr:AttributeError:模块"requests"没有属性"GET","2020-04-16T11:29:34.215734Z
stderr:"]
你能帮忙吗?谢谢
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)
}