使用POST http请求创建Asana任务

时间:2017-03-28 04:24:08

标签: javascript asana asana-api

我尝试使用asana-api使用POST http请求创建任务,但我一直收到 400错误请求作为回复

我设法使用(一个GET请求)从Asana-api获取数据,但我在向Asana发送数据时遇到问题(POST请求)

我正在使用'请求'模块进行api调用

这里出现错误消息:

`{"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"}
 ]}`

这是我的代码:

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);
     }

}

我也尝试了不同的实现:

  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);
     }

}

1 个答案:

答案 0 :(得分:1)

根据请求模块提供的示例,您的选项对象似乎使用payload作为键,但它应该是body