【发布时间】:2022-01-24 23:59:53
【问题描述】:
我正在创建一个触发 Cloud Run 服务端点的 HTTP 任务。正在从队列中调用处理程序,但我无法获取有效负载。
我尝试记录标题,但它似乎不包含 content-type,我怀疑这就是 app.use(bodyParser.raw({ type: "application/octet-stream" })); 失败的原因。
根据我的 Express 处理程序,这些是我收到的所有请求标头:
{
"host": "my_service.a.run.app",
"x-cloudtasks-queuename": "notifications",
"x-cloudtasks-taskname": "36568403927752792701",
"x-cloudtasks-taskretrycount": "0",
"x-cloudtasks-taskexecutioncount": "0",
"x-cloudtasks-tasketa": "1640337087",
"authorization": "Bearer some_token",
"content-length": "193",
"user-agent": "Google-Cloud-Tasks",
"x-cloud-trace-context": "496573f34310f292ade89f566e7c8f40/11132544205299294705;o=1",
"traceparent": "00-496573f34310f292ade89f566e7c8f40-9a7ebdf8d332a1f1-01",
"x-forwarded-for": "35.187.132.21",
"x-forwarded-proto": "https",
"forwarded": "for=\"35.187.132.21\";proto=https",
"accept-encoding": "gzip, deflate, br"
}
这是当前处理程序的样子:
app.post("/send-notification", (req, res) => {
console.log(`req.headers: ${JSON.stringify(req.headers)}`);
console.log(`req.body: ${JSON.stringify(req.body)}`);
});
对于正文,它打印{},但应该有一个有效载荷。我是这样创建的:
const task = {
httpRequest: {
httpMethod: "POST" as const,
url: def.url,
oidcToken: {
serviceAccountEmail,
},
body: Buffer.from(JSON.stringify(payload)).toString("base64"),
},
scheduleTime: {
seconds: 3 + Date.now() / 1000,
},
};
我已经没有什么想法可以尝试了。我错过了什么?
【问题讨论】:
-
能否在任务定义中添加 Content-Type 标头?它解决了你的问题吗?
-
文档中没有提到,但我看到Python example 将 application/json 设置为 content-type 标头,因此至少看起来是可能的。我要试一试...
-
@guillaumeblaquiere 太好了,解决了!我已将其设置为“application/json”并在服务器上添加了
app.use(express.json())。谢谢?????? -
我看到了同样的例子!我在答案中添加了解决方案。
标签: google-cloud-platform google-cloud-run google-cloud-tasks