| 37 | const client = new CloudTasksClient(); |
| 38 | |
| 39 | async function createTask() { |
| 40 | // TODO(developer): Uncomment these lines and replace with your values. |
| 41 | // const project = 'my-project-id'; |
| 42 | // const queue = 'my-appengine-queue'; |
| 43 | // const location = 'us-central1'; |
| 44 | // const payload = 'Hello, World!'; |
| 45 | |
| 46 | // Construct the fully qualified queue name. |
| 47 | const parent = client.queuePath(project, location, queue); |
| 48 | |
| 49 | const task = { |
| 50 | appEngineHttpRequest: { |
| 51 | headers: { |
| 52 | 'Content-Type': 'text/plain', // Set content type to ensure compatibility your application's request parsing |
| 53 | }, |
| 54 | httpMethod: 'POST', |
| 55 | relativeUri: '/log_payload', |
| 56 | }, |
| 57 | }; |
| 58 | |
| 59 | if (payload) { |
| 60 | task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64'); |
| 61 | } |
| 62 | |
| 63 | if (inSeconds) { |
| 64 | // The time when the task is scheduled to be attempted. |
| 65 | task.scheduleTime = { |
| 66 | seconds: inSeconds + Date.now() / 1000, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | console.log('Sending task:'); |
| 71 | console.log(task); |
| 72 | |
| 73 | // Send create task request. |
| 74 | const request = {parent: parent, task: task}; |
| 75 | const [response] = await client.createTask(request); |
| 76 | const name = response.name; |
| 77 | console.log(`Created task ${name}`); |
| 78 | } |
| 79 | |
| 80 | createTask(); |
| 81 | // [END cloud_tasks_appengine_create_task] |