无法从 Cloud Tasks HTTP 请求中获取 content-type 或 body
Unable to get content-type or body from Cloud Tasks HTTP request
我正在创建一个触发云 运行 服务端点的 HTTP 任务。正在从 queue 调用处理程序,但我无法获取有效负载。
我尝试记录 headers,但它似乎不包含 content-type
,我怀疑这就是 app.use(bodyParser.raw({ type: "application/octet-stream" }));
失败的原因。
这些是所有请求headers 我根据我的 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)}`);
});
对于 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,
},
};
我 运行 想不出要尝试的东西。我错过了什么?
根据 example in the documentation,可以将 header 添加到对 Cloud Task 执行的请求中。
您可以在header
中添加内容类型
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,
},
}
我正在创建一个触发云 运行 服务端点的 HTTP 任务。正在从 queue 调用处理程序,但我无法获取有效负载。
我尝试记录 headers,但它似乎不包含 content-type
,我怀疑这就是 app.use(bodyParser.raw({ type: "application/octet-stream" }));
失败的原因。
这些是所有请求headers 我根据我的 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)}`);
});
对于 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,
},
};
我 运行 想不出要尝试的东西。我错过了什么?
根据 example in the documentation,可以将 header 添加到对 Cloud Task 执行的请求中。
您可以在header
中添加内容类型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,
},
}