Search code examples
pythonpython-requestsasanazapierasana-api

Asana bad request when attempting to set custom field values (API)


I am attempting to create a new task using the Asana API with https://app.asana.com/api/1.0/tasks endpoint. I have been successful in creating some test tasks in the desired project. However I am now trying to also set the values of the custom fields and seem to be hitting a road block.

I am using the Python request library. I have a feeling this is an issue with the way I have formatted the post. I have checked a number of sources, including here, as well tinkered and modified the way in which I structure the "options" dictionary. However it does not seem to help.

def postTaskToAsana(taskName, taskURL, taskCreated, taskCompleted, taskDue):

token = "<TOKEN>"
bearerToken = "Bearer " + token
header = {
    "Authorization" : bearerToken
    }

options = {
    "projects" : ["234234234"],
    "name" : "Hello, World!",
    "notes" : "How are you",
    "assignee" : "2342342342",
    "custom_fields" : { "234234234234" : "hello" }
    }

url = "https://app.asana.com/api/1.0/tasks"
r = requests.post(url, headers=header, data=options)
return r

If I remove the "custom_fields" from the options dictionary above then the post request works and I can see the newly created task. The response I get back from the above code is:

{"errors":[{"message":"Oops! An unexpected error occurred while processing this request. The input may have contained something the server did not know how to handle. For more help, please contact api-support@asana.com and include the error phrase from this response.","phrase":"9 "}]}

As noted in this post the goal is to generate JSON for the request in this format:

{
"data" : {
    "custom_fields" : { "2342342342" : "INFO" }
    }
}

Which as far as I can tell is what my code should be doing.

Any help on this would be great, thanks.


Solution

  • [RESOLVED]

    As suspected the issue had to do with the way in which I was formatting my request. For reasons I am not clear on the additional level created by the nested custom_fields dictionary was not being formatted in correct JSON format. I was able to resolve this using requests JSON parameter.

    def postTaskToAsana(taskName, taskURL, taskCreated, taskCompleted, taskDue):
    
    token = "<TOKEN>"
    bearerToken = "Bearer " + token
    header = {
        "Authorization" : bearerToken
        }
    
    options = {
        "data" : {
            "projects" : ["123412341234"],
            "name" : "Review Task: " + taskName,
            "notes" : "Please review this task for where the process failed.\nTask: " + taskURL,
            "assignee" : "123412341234",
            "followers" : ["123412341234"],
            "custom_fields" : {
                "123412341234" : taskCreated,
                "123412342134" : taskDue,
                "123412341234" : taskCompleted
            }
        }
    }
    
    url = "https://app.asana.com/api/1.0/tasks"
    r = requests.post(url, headers=header, json=options)
    return r