我已经将用Python编写的云函数部署到GCP中。
from flask import escape
import functions_framework
@functions_framework.http
def hello_http(request):
"""HTTP Cloud Function.
Args:
request (flask.Request): The request object.
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
"""
request_json = request.get_json(silent=True)
request_args = request.args
if request_json and 'name' in request_json:
name = request_json['name']
elif request_args and 'name' in request_args:
name = request_args['name']
else:
name = 'World'
return 'Hello {}!'.format(escape(name))
例如,我可以使用axios
从角度调用这个函数。然而,没有通过发送数据。如果我在python函数中打印request.data
,它就会打印空。而且,request_json
总是None
。
axios.post('https://projektid.cloudfunctions.net/hello_http', {
data: 123
})
.then((result: any) => {
console.log(result)
})
.catch((err) => {
console.log('ERROR', err)
});
编辑:如果我像这样从Python调用它,它可以很好地工作,并传递参数:
import requests
r = requests.post('https://projektid.cloudfunctions.net/hello_http', json={'data': 123})
print(r.text)
如何正确地将数据从角/类型记录传递给python函数?
发布于 2022-04-21 17:36:19
如果要从本地主机调用函数,请确保正在处理cors请求。您可以在文献资料中找到有关这方面的更多信息。
https://stackoverflow.com/questions/71954621
复制相似问题