1

I'm trying to remove the value of a custom field on an asana task via a PUT request.

Based on the original json data I sent over to create the task with a custom field value and the documentation here this is my best guess for how this should look:

let data = {custom_fields: { ASANA_CUSTOM_FIELD_ID_NUMBER: null }}; //struggling here
updateTask(ASANA_TASK_ID_NUMBER, data);


function updateTask(id, data) {
  return put(`https://app.asana.com/api/1.0/tasks/${ASANA_TASK_ID_NUMBER}`, data);
}

function put(url, data) {
  return makeRequest({
    "url": url,
    "headers": {"Authorization": "Bearer " + accessCode()},
    "type": "PUT",
    "data": data
  })
}

But I get the error:

status:400 Bad request 
custom_fields: Value cannot be an array: []

Which seems verbose enough to solve except I've tried every format i can come up with and I've had no luck working it out. I know that the put function works fine for updating other fields for a task and I see the same error with an actual number other than null.

Holly Grant
  • 121
  • 1
  • 5
  • Can you please run this request with `curl -v`. This will make it much easier to try and reproduce your issue. Your request may be sending formdata instead of JSON, which could cause an object to get parsed as an empty array. – Jeff May 03 '17 at 17:30

2 Answers2

0

You will need to send your content in JSON rather than urlencoded data. This is a bit of a bug in Asana API in my opinion. They say that they support form-encoded content however it doesn't like it when you try to send an object as it thinks it's an array.

brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
0

I'm not sure why, but setting custom fields seems to be different from the rest of the API requests.

Here is some code that works for setting it, you can probably figure out how to apply this to whatever language you're using:

function SetCustomField(taskId, fieldId, value) {
  // not sure why, but to set the custom task you have to tell it your content type is json,
  // then send json as a string instead of letting it serialize it for you
  var headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer API-KEY'
  };

  var formData = '{"data": { "custom_fields": { "' + fieldId + '": ' + value + '} } }';

  var options = {
    'method': 'put',
    'headers': headers,
    'payload': formData
  };
  var response = UrlFetchApp.fetch('https://app.asana.com/api/1.0/tasks/' + taskId, options);
  //Logger.log(response.getContentText());
}
tayl0rs
  • 561
  • 2
  • 5
  • 14
  • Perhaps there has been some update to the API, I am passing a Json that looks like this: {'data': {'custom_fields': {'1203012693580045': '1203012693586629'}}} I also tried with the cooresponding name for the option: {'data': {'custom_fields': {'1203012693580045': '1 Month or ++'}}} Both return: "ValueError: dictionary update sequence element #0 has length 1; 2 is required" From that I get that the "data" element should also have some other pointer in the parameter sent. Any one knows? – Carlo A. Fernandez B. Sep 21 '22 at 07:34