我有一个运行 Python 3.7 的 App Engine 服务,需要通过以下方式调用我的云函数之一并获取响应https.oncall
扳机。
我想我可以通过以下方式做到这一点:
import logging
from sys import exit
import firebase_admin
import requests
import google.cloud.logging
client = google.cloud.logging.Client()
client.setup_logging()
firebase_admin.initialize_app()
response = requests.post("https://us-central1-myproject.cloudfunctions.net/functionname", data={"foo": "bar"})
if response.status_code != 200:
exit("Could not call function! :(") # This is happening
logging.info(f"Yay! Here is the result: {response.text}")
# do something else
exit()
然而,我得到Request has incorrect Content-Type.
来自云函数。
我尝试了请求语法的多种变体,例如:data='{"foo": "bar"}'
and data=json.dumps({"foo": "bar"})
但我能得到的只是Request has incorrect Content-Type.
or Request has incorrect Content-Type. application/x-www-form-urlencoded
.
如何正确地将我的字典附加到我的请求中,以便该函数以以下形式接收它application/json
如前所述在云函数文档中 https://firebase.google.com/docs/functions/callable-reference?
我读过其他建议使用的帖子json.dumps()
函数会告诉request
to use application/json
但 Cloud Functions 对此并不满意。
你没有设置Content-Type
为了您的要求。从...开始requests==2.4.2
,您可以使用json
参数而不是传递字典并自动设置Content-Type
:
requests.post(url, json={"foo": "bar"})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)