Error with create task endpoint

Hi all,

Asana is giving me this error in my application:

"{"errors":[
	{"message":"Request data must be a JSON object, not null","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"
	}]
}"

The thing is, do I really need to send my data in JSON to API?

If i do it with curl in a terminal, it works fine:

curl -H "Authorization: Bearer 0/personal_token" https://app.asana.com/api/1.0/tasks --data-urlencode "assignee=blabla@blublu.com" --data-urlencode "notes=Hola mundo today?" --data-urlencode "name=Hello, world!" --data-urlencode "workspace=123456789"

As far as I know about my IDE (Qt creator and C++) this can be done using:

//Setting Header
request.setRawHeader(QByteArray("Authorization"), "Bearer 0/access_token");
//Codifing Header
request.setHeader(request.ContentTypeHeader,"Content-Type: application/x-www-form-urlencoded");
//Parameters of the request URL
postData.addQueryItem("assignee", "blablabla@blublublu.com");
postData.addQueryItem("notes", "Tarea de Prueba ASANA");
postData.addQueryItem("name", "PRIMERA TAREA");
postData.addQueryItem("workspace", "123456789");

Wich worked fine during my oauth process.

The thing is, I think I am wrong because “–data-urlencode” doesn’t join fields in a single URL and this curl (The one I perform in my IDE) doesent work:

curl -v -H "Authorization: Bearer 0/access_token" https://app.asana.com/api/1.0/tasks?assignee=dadada@bububu.com&notes=Tarea%20de%20Prueba%20ASANA&name=PRIMERA%20TAREA&workspace=123456

Which is the correct form of making a POST request (JSON or URL)?

Regards,

Daniel.

While it can theoretically be done via URL query string parameters in most cases, there are some cases in which you’ll run into trouble, and the recommended way is via JSON in a message body.

2 Likes

Hi Phil,

Ok, I’ve changed my code in order to send a JSON data:

   //Parameters of the JSON to make a POST
    jsonSendData.insert("assignee", QJsonValue::fromVariant("blabla@bleble.com"));
    jsonSendData.insert("notes", QJsonValue::fromVariant("Tarea de Prueba ASANA"));
    jsonSendData.insert("name", QJsonValue::fromVariant("PRIMERA TAREA"));
    jsonSendData.insert("workspace", QJsonValue::fromVariant("123456"));

And this is what Asana says:

POST answer: "{\"errors\":[{\"message\":\"Unrecognized request field 'assignee' The only allowed keys at the top level are: data, options, auth. Is it possible you did not wrap object properties in a 'data' object?\",\"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 there any specific format for the JSON file (I mean structure)? The one I’m sending looks ok to me (Same “format” that my ide shows in Asana responses):

JSON data: “{\n "assignee": "blabla@bleble.com",\n "name": "PRIMERA TAREA",\n "notes": "Tarea de Prueba ASANA",\n "workspace": "123456"\n}\n”

Regards,

Dani.

I Phil,

At this point I’m able to answer myself hahahahaha. I was sending only the fields by their own:

{
    	"name":"PRIMERA TAREA",
    	"notes":"Tarea de Prueba ASANA",
    	"workspace": "123456"
    	}

I must “encapsulate” JSON fields in a “data” structure. I mean, body must be like:

{
"data": {
	"name":"PRIMERA TAREA",
	"notes":"Tarea de Prueba ASANA",
	"workspace": "123456"
	}
}

This is working right know, I have my task on my history and my app is not crashing, so we can call it a victory!

Thank you very much.

Regards,

Dani

3 Likes