我正在创建一个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,
},
};我想不出要尝试的东西了。我遗漏了什么?
发布于 2021-12-24 14:45:53
根据该文档中的示例,可以向Cloud执行的请求添加标头。
可以在标题中添加内容类型。
const task = {
httpRequest: {
httpMethod: "POST" as const,
url: def.url,
headers: {
"Content-type": "application/json"
},
oidcToken: {
serviceAccountEmail,
},
body: Buffer.from(JSON.stringify(payload)).toString("base64"),
},
scheduleTime: {
seconds: 3 + Date.now() / 1000,
},
}https://stackoverflow.com/questions/70471448
复制相似问题