zyx
zyx

Reputation: 131

Sending a JSON payload to a Google Cloud Task

I'm likely missing something incredibly obvious, but in the project I'm working on I've got to send many jobs from a CSV of info to be processed asynchronously and Google App Engine's current way is through their new (beta) Cloud Tasks mechanism.

It will accept a payload as part of the task, so I was going to send a JSON array with each job's pertinent data... except that the only way to dictate the "Content-Type: application/json" header is during creation of the task object.

I'm using Google's own cloud-tasks 0.5.0 library.

Here is what I've been attempting, since it seems this is how most other non-cURL HTTP POST requests would accept the Content-Type header...

require_once 'vendor/autoload.php';

use Google\Cloud\Tasks\V2beta3\AppEngineHttpQueue;
use Google\Cloud\Tasks\V2beta3\CloudTasksClient;
use Google\Cloud\Tasks\V2beta3\Queue;
use Google\Cloud\Tasks\V2beta3\Task;

<<< ...lots of cruft omitted... >>>

        $json_payload = json_encode(
            array(
                "batch" => $operation_time,
                "order" => $csvln[0],
                "customer" => $csvln[1],
                "email" => $csvln[2],
                "salesperson" => $csvln[3]
            )
        );

        //Create each of the tasks in the queue
        $options = [
            'http' => [
                'header'  => "Content-type: application/json",
                'method'  => 'POST',
                'content' => $json_payload
            ]
        ];

        $task = new Task($options);

Any help would be immensely appreciated!

Upvotes: 1

Views: 4473

Answers (1)

K F
K F

Reputation: 1546

You can load a task into the Task Queue with a pre-defined payload using an App Engine HTTP Request from the Cloud Tasks PHP Client Library.

After you defined the Task, you can use the setter methods provided to you by AppEngineHttpRequest to construct your HTTP object with any required headers. This will also allow to assign the payload.

Below is a simple snippet showing how to attach a task with a payload to the default queue:

use Google\Cloud\Tasks\V2beta3\AppEngineHttpRequest;
use Google\Cloud\Tasks\V2beta3\HttpMethod;
use Google\Cloud\Tasks\V2beta3\Task;

//Preparing the payload
$json_payload = json_encode(
    array(
        "batch"       => date("h:i:sa"),
        "order"       => "Payload-0000",
        "customer"    => "Payload-0001",
        "email"       => "Payload-0002",
        "salesperson" => "Payload-0003"
    )
);

//Create and configure the task   
$httpR=new AppEngineHttpRequest();
$httpR->setBody($json_payload);
$httpR->setHeaders(['Content-type'=>'application/json']);
$httpR->setHttpMethod(HttpMethod::POST);   
$httpR->setRelativeUri("/example_task_handler");

$task = new Task();
$task->setAppEngineHttpRequest($httpR);

Also consider updating your library as the current version is v0.86.0 which it will allow the assignation of headers even after the creation of the task object.

Upvotes: 3

Related Questions