从应用程序引擎运行时Python 3.7调用云函数
我有一个运行Python3.7的应用引擎服务,需要通过从应用程序引擎运行时Python 3.7调用云函数,python,google-app-engine,google-cloud-platform,python-requests,google-cloud-functions,Python,Google App Engine,Google Cloud Platform,Python Requests,Google Cloud Functions,我有一个运行Python3.7的应用引擎服务,需要通过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
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()
但是,我从云函数得到的请求的内容类型不正确。
我在请求语法上尝试了多种变体,例如:data='{“foo”:“bar”}
和data=json.dumps({“foo”:“bar”})
,但我能得到的只是请求的内容类型不正确。
或请求的内容类型不正确。应用程序/x-www-form-urlencoded
如何正确地将字典附加到请求中,以便函数以application/json
的形式接收请求
我读过其他帖子建议使用
json。dumps()
函数会告诉request
使用application/json
,但云函数对此并不满意。您没有为您的请求设置内容类型。从requests==2.4.2
开始,您可以使用json
参数来传递字典并自动设置内容类型
:
requests.post(url,json={“foo”:“bar})