Batch API - Updating tasks in bulk

tasks

#1

I’m trying to update tasks in bulk using Batch API

I’m passing JSON as the body in a php script like this:

function UpdateTasks() {
    
$aToken = '[REDACTED]';
      
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://app.asana.com/api/1.0/batch",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 90,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => json_encode($GLOBALS['amz']),
      CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer " . $aToken
      ),
    ));

    $response = curl_exec($curl);
    $jArray = json_decode($response, true);

    $err = curl_error($curl);
    $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    curl_close($curl);

    if ($err) {
      echo "\n cURL Error #:" . $err;
    }
    else
    {
        echo "Status Code: <br>" . $http_status . "<br> Response <br>" . $response;
    }

}

However, Asana throws the below error:

Status Code: 
400

Response 
{"errors":[{"message":"Could not interpret {\"data\":{\"actions\":[{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/829561517314688\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - UK\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710721\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Canada\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710714\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - France\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710715\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Germany\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710716\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Italy\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710717\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Spain\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710719\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - USA\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710720\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - China\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710718\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Mexico\"}}]}} as an identifier in {\"data\":{\"actions\":[{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/829561517314688\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - UK\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710721\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Canada\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710714\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - France\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710715\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Germany\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710716\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Italy\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710717\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Spain\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710719\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - USA\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710720\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - China\"}},{\"method\":\"put\",\"relative_path\":\"\\/tasks\\/833683133710718\",\"data\":{\"completed\":\"false\",\"name\":\"[]unlinked on Trimming Shop - Mexico\"}}]}}.","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}

Is this because I’m passing a encode_json value to the call and asana expects a json file?
Also, what exactly is the “Could not interpret” here referring to?

I’ve compared my JSON to the example mentioned at

and it looks correct.

I’m at my wits end. Any help is much appreciated.
Kind regards.


#2

Hi @Navik, from a quick glance at this, it looks like you’re omitting the Content-Type header and so our API is not interpreting your body as JSON. Can you try adding a Content-Type: application/json header to your request and sending it again?


#3

Thanks @Joe_Trollo,

I’ll surely add this. Also, I’m extremely sorry that I missed the token in my code. I always make sure to remove sensitive information but I’m not sure how this happened.

Presuming, long working hours. I’ll let you know how this goes.


#4

Status: 200

Thank you very much. That worked.