0

I'm trying to use the asana-api to create a Task using a POST http request but I keep getting a 400 bad request as a response.

I managed to get data from the Asana-api using ( a GET request ), but I'm having trouble sending data to Asana with ( a POST request )

I'm using the 'request' module to do the api call

here's the error message :

`{"errors":[{
      "message":"Could not parse request data,invalid JSON",
      "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"}
 ]}`

Here's my code:

testTask(){
   var taskName = "Test Name for a Test Task"
   var workspaceID = "123456789"
   var projectID = "123456789"
   var assigneeID = "123456789"
   var parentID = null
   this.createTask(taskName, workspaceID, projectID, assigneeID, parentID)
}

createTask(taskName, workspaceID, projectID, assigneeID, parentID){
    var token = "0/1234abcd5678efgh9102ijk"
    var bearerToken = "Bearer " + token
    var task = {
       data: {
         assignee: "me",
         notes: "test test test test",
         workspace: workspaceID,
         name: taskName,
         projects: [projectID],
         parent: parentID
       }
     }
     var options = {
       "method" : "POST",
       "headers" : {"Authorization": bearerToken},
       "contentType": "application/json",
       "payload" : JSON.stringify(task)
     }
     try {
       var url = "https://app.asana.com/api/1.0/tasks";
       request.post(url, options, function optionalCallback(err, httpResponse, body) {
       if (err) {
          return console.error('upload failed:', err);
       }
          console.log('Upload successful!  Server responded with:', body);
       });
     }
     catch (e) {
          console.log(e);
     }

}

I also tried a different implementation :

  createTask(){
    var token = "0/1234abcd5678efgh9102ijk"
    var bearerToken = "Bearer " + token

     var options = {
       "method" : "POST",
       "headers" : {"Authorization": bearerToken},
     }
     try {
       request.post("https://app.asana.com/api/1.0/tasks?workspace=1234567&projects=765534432&parent=null&name=taskName&assignee=me", options, function optionalCallback(err, httpResponse, body) {
       if (err) {
          return console.error('upload failed:', err);
       }
          console.log('Upload successful!  Server responded with:', body);
       });
     }
     catch (e) {
          console.log(e);
     }

}
AziCode
  • 2,510
  • 6
  • 27
  • 53

1 Answers1

1

Based on the examples provided by the request module, it appears that your options object uses payload as a key, but it should be body.

Jeff
  • 456
  • 2
  • 5
  • I updated my question by adding another implementation that I tried, where I didn't use a payload and I got the same error – AziCode Mar 29 '17 at 00:28
  • 1
    I'm not familiar with this module, so may not be the best help to troubleshoot it. Have you tried using the [Asana node client library](https://github.com/Asana/node-asana)? Here is an example of a CURL request that works to POST a task: `curl --request POST -H "Authorization: Bearer 0/c12345" \ https://app.asana.com/api/1.0/tasks \ -d "workspace"="12345" \ -d "name"="testing task creation"` – Jeff Mar 29 '17 at 00:48
  • 1
    You cannot do a POST request and put the data in the query parameters. They need to be included in the body. "body": JSON.stringify(task) – Mark Mar 29 '17 at 01:11
  • @Mark great thanks, could you please answer the question, so that I can select it as being the right answer to help other folks who might encounter the same issue. – AziCode Mar 29 '17 at 03:10
  • 1
    Jeff's answer here is exactly the answer. – Mark Mar 29 '17 at 03:54
  • @Jeff can you please have a look at this question, I'm having issues with custom_fields: http://stackoverflow.com/questions/43129086/how-to-set-custom-fields-using-a-post-http-request – AziCode Mar 30 '17 at 21:59