gpt4 book ai didi

javascript - 谷歌云任务不将正文发送到http云函数

转载 作者:行者123 更新时间:2023-12-02 22:08:24 25 4
gpt4 key购买 nike

我有一个 gcloud 任务,其中的代码大部分是从云任务文档中复制的。 https://cloud.google.com/tasks/docs/creating-http-target-tasks

云函数的目标是能够将 dateTo 和 dateFrom 日期推送到它,以便它可以循环周期并从中创建 cloudTasks。我还没有创建循环,因为我首先希望解决这个问题。

问题是它没有推送http云函数的主体。http云功能在使用CURL时起作用。

curl -X POST "posturl"-H "Content-Type:application/json"--data '{"date": "2019-12-01", "lastRun": false}'

我检查了这里提到的方法,它是POST,所以应该没问题。 https://stackoverflow.com/a/56448479/2785289

检查接口(interface)没有有效负载。使用 gcloud beta 描述任务...没有正文,也没有关于有效负载的内容。

httpRequest:
headers:
User-Agent: Google-Cloud-Tasks
httpMethod: POST
url: correcthttpurl
name: name
scheduleTime: '2020-01-07T15:45:24.774042Z'
view: view
scheduleTime: '2020-01-07T15:45:24.774042Z'
view: BASIC

这是云函数创建任务的代码。任务已添加到队列中,但单击运行时它们似乎没有触发该功能。 (可能是因为该功能需要 body 才能工作)

/**
* Background Cloud Function to be triggered by Pub/Sub.
* This function is exported by index.js, and executed when
* the trigger topic receives a message.
*
* @param {object} pubSubEvent The event payload.
* @param {object} context The event metadata.
*/

// gcloud functions deploy queueAffiliateApiTasks --trigger-topic queue-affiliate-api-tasks --region europe-west1 --runtime=nodejs8

const moment = require("moment");


exports.queueAffiliateApiTasks = async (pubSubEvent, context) => {
const data =
pubSubEvent.data || Buffer.from(pubSubEvent.data, "base64").toString();

const attributes = pubSubEvent.attributes;

// take 30 days ago untill yesterday
let dateFrom = moment().subtract(1, "days");
let dateTo = moment().subtract(1, "days");

// if dates provided in pubsub use those
if (attributes && "dateFrom" in attributes && "dateTo" in attributes) {
console.log("with attributes");
dateFrom = attributes.dateFrom;
dateTo = attributes.dateTo;
} else {
console.log("no attributes");
}

console.log(dateFrom);
console.log(dateTo);

// use dates for looping
dateFrom = moment(dateFrom);
dateTo = moment(dateTo);

console.log(dateFrom);
console.log(dateTo);

const date = dateTo.format("YYYY-MM-DD").toString();
const lastRun = false;
const url =
"the correct url to the http cloud function";
const payload = JSON.stringify({ date: date, lastRun: false }, null, 2);

await createHttpTask(url, payload);
};

async function createHttpTask(url, payload) {
const project = "xxx";
const queue = "affiliate-api-queue";
const location = "europe-west1";
const inSeconds = 0 // Delay in task execution
// [START cloud_tasks_create_http_task]
// Imports the Google Cloud Tasks library.
const {CloudTasksClient} = require('@google-cloud/tasks');

// Instantiates a client.
const client = new CloudTasksClient();

// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);

const task = {
httpRequest: {
httpMethod: 'POST',
url,
},
};

task.httpRequest.body = Buffer.from(payload).toString('base64');

if (inSeconds) {
// The time when the task is scheduled to be attempted.
task.scheduleTime = {
seconds: inSeconds + Date.now() / 1000,
};
}

// Send create task request.
console.log('Sending task:');
console.log(task);
const request = {parent, task};
const [response] = await client.createTask(request);
console.log(`Created task ${response.name}`);
console.log(`Response: ${JSON.stringify(response.httpRequest, null, 2)}`);
// [END cloud_tasks_create_http_task]
}

我错过了什么?

enter image description here

可以看到任务已添加但未执行 enter image description here有效负载和 header 为空

最佳答案

根据 Google docs ,默认responseView是BASIC;默认情况下,并非所有信息都会被检索,因为某些数据(例如有效负载)可能需要仅在需要时返回,因为其大小较大或其包含的数据的敏感性。

关于javascript - 谷歌云任务不将正文发送到http云函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59642035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com