* Create a task with an HTTP target for a given queue with an arbitrary payload.
( project = 'my-project-id', // Your GCP Project id queue = 'my-appengine-queue', // Name of your Queue location = 'us-central1', // The GCP region of your queue url = 'https://example.com/taskhandler', // The full url path that the request will be sent to payload = 'Hello, World!', // The task HTTP request body inSeconds = 0 // Delay in task execution )
| 23 | * Create a task with an HTTP target for a given queue with an arbitrary payload. |
| 24 | */ |
| 25 | function main( |
| 26 | project = 'my-project-id', // Your GCP Project id |
| 27 | queue = 'my-appengine-queue', // Name of your Queue |
| 28 | location = 'us-central1', // The GCP region of your queue |
| 29 | url = 'https://example.com/taskhandler', // The full url path that the request will be sent to |
| 30 | payload = 'Hello, World!', // The task HTTP request body |
| 31 | inSeconds = 0 // Delay in task execution |
| 32 | ) { |
| 33 | // [START cloud_tasks_create_http_task] |
| 34 | // Imports the Google Cloud Tasks library. |
| 35 | const {CloudTasksClient} = require('@google-cloud/tasks'); |
| 36 | |
| 37 | // Instantiates a client. |
| 38 | const client = new CloudTasksClient(); |
| 39 | |
| 40 | async function createHttpTask() { |
| 41 | // TODO(developer): Uncomment these lines and replace with your values. |
| 42 | // const project = 'my-project-id'; |
| 43 | // const queue = 'my-queue'; |
| 44 | // const location = 'us-central1'; |
| 45 | // const url = 'https://example.com/taskhandler'; |
| 46 | // const payload = 'Hello, World!'; |
| 47 | // const inSeconds = 180; |
| 48 | |
| 49 | // Construct the fully qualified queue name. |
| 50 | const parent = client.queuePath(project, location, queue); |
| 51 | |
| 52 | const task = { |
| 53 | httpRequest: { |
| 54 | headers: { |
| 55 | 'Content-Type': 'text/plain', // Set content type to ensure compatibility your application's request parsing |
| 56 | }, |
| 57 | httpMethod: 'POST', |
| 58 | url, |
| 59 | }, |
| 60 | }; |
| 61 | |
| 62 | if (payload) { |
| 63 | task.httpRequest.body = Buffer.from(payload).toString('base64'); |
| 64 | } |
| 65 | |
| 66 | if (inSeconds) { |
| 67 | // The time when the task is scheduled to be attempted. |
| 68 | task.scheduleTime = { |
| 69 | seconds: parseInt(inSeconds) + Date.now() / 1000, |
| 70 | }; |
| 71 | } |
| 72 | |
| 73 | // Send create task request. |
| 74 | console.log('Sending task:'); |
| 75 | console.log(task); |
| 76 | const request = {parent: parent, task: task}; |
| 77 | const [response] = await client.createTask(request); |
| 78 | console.log(`Created task ${response.name}`); |
| 79 | } |
| 80 | createHttpTask(); |
| 81 | // [END cloud_tasks_create_http_task] |
| 82 | } |
no test coverage detected