Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从应用程序引擎运行时Python 3.7调用云函数_Python_Google App Engine_Google Cloud Platform_Python Requests_Google Cloud Functions - Fatal编程技术网

从应用程序引擎运行时Python 3.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

我有一个运行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_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})