我刚刚在Beta Python 3.7运行时编写了一个带有HTTP触发器的Google Cloud函数.现在我想知道调用函数时如何将字符串变量传递给函数.我已经看过文档了,但还没有找到任何相关信息.

我的触发器类似于:

https://us-central1-*PROJECT_ID*.cloudfunctions.net/*FUNCTION_NAME*

我是否误解了云功能的工作原理?你能把变量传递给他们吗?

推荐答案

将变量传递给函数的方式与将变量传递给任何URL的方式相同:

1. Via a GET with query parameters:

def test(request):
    name = request.args.get('name')
    return f"Hello {name}"
$ curl -X GET https://us-central1-<PROJECT>.cloudfunctions.net/test?name=World
Hello World

2. Via a POST with a form:

def test(request):
    name = request.form.get('name')
    return f"Hello {name}"
$ curl -X POST https://us-central1-<PROJECT>.cloudfunctions.net/test -d "name=World"
Hello World

3. Via a POST with JSON:

def test(request):
    name = request.get_json().get('name')
    return f"Hello {name}"
$ curl -X POST https://us-central1-<PROJECT>.cloudfunctions.net/test -d '{"name":"World"}'
Hello World

更多细节可在这里找到:https://cloud.google.com/functions/docs/writing/http

Python-3.x相关问答推荐

如何在输入正确的用户名和密码时添加按钮?

Pandas教程:如何更新行内数值的位置

TypeError: issubclass() arg 1 在 Flask 中导入 langchain 时必须是一个类

在 pytest 中,如何测试 sys.exit('some error message')?

attrs 将 list[str] 转换为 list[float]

为什么 numpy 的 `np.char.encode` 会将一个空的 unicode 数组变成一个空的 `float64` 数组?

如何在 on_ready 事件中使用 change_presence? (discord.py)

是否将dict转换为一个数据帧,每个值都有重复的键?

请求:RecursionError:超出最大递归深度

基本 Flask 应用程序未运行(TypeError:模块中缺少必填字段type_ignores)

如何确定一个类的元类?

multiprocessing.Queue 中的 ctx 参数

Linux Mint 上的 Python3 错误没有名为蓝牙的模块

Asyncio RuntimeError:事件循环已关闭

类型提示返回 NameError: name 'datetime' not defined

如何使用 Python 订阅 Websocket API 通道?

Python 3 - Zip 是 pandas 数据框中的迭代器

没有名为urlparse的模块,但我没有使用 urlparse

注册 Celery 基于类的任务

在 PyCharm 中配置解释器:请使用不同的 SDK 名称