Having difficulty creating a Task via API

Background: JavaScript (V8) code being controlled by C# via ClearScript. It’s a project called Lychen.

This is the code creating the JSON packet:

    // ct - create task - ct/project/assignee/name/notes

var Console = CS.System.Console;
var DateTime = CS.System.DateTime;
var File = CS.System.IO.File;
var Path = CS.System.IO.Path;

if (ARGUMENTS.length < 5) {
    Console.WriteLine("{0} needs four parameters (/project/assignee/name/notes)", ARGUMENTS[0]);
} else {
    REQUEST = new RESTSHARP.RestRequest("api/1.0/tasks", RESTSHARP.Method.POST);
    var token = ASANA_SETTINGS.IniReadValue("Authorisation", "Token", "");
    if (token === "") {
        Console.WriteLine("Token not found in AsanaSettings.INI");
    } else {
        REQUEST.AddHeader("Authorization", "Bearer " + token);
        REQUEST.AddHeader("Content-Type", "application/json");
        var json = JSON.stringify({
            projects: [{ gid: ARGUMENTS[1] }],
            assignee: { gid: ARGUMENTS[2] },
            name: ARGUMENTS[3],
            notes: ARGUMENTS[4],
            workspace: { gid: WORKSPACE }
        });
        REQUEST.AddJsonBody(json);
        RESPONSE = CLIENT.Execute(REQUEST);

        var tag = DateTime.Now.ToString("yyyy'-'MM'-'dd'-'HH'-'mm");
        File.WriteAllText(Path.Combine("log", "ct_" + tag + ".json"), RESPONSE.Content);

    }
}

This is the packet of JSON:

{"projects":[{"gid":"1113266773900829"}],"assignee":{"gid":"9814050857874"},"name":"Task 1","notes":"Added we assume to Test Project","workspace":{"gid":"9814050864025"}}

And this is the error message:

{"errors":[{"message":"You should specify one of workspace, parent, projects","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"}]}

What I don’t get is that there is a projects and a workspace. And it’s not a subtask so I don’t need a parent (and I have tried putting a null parent in to no effect.)

Where’s the problem?

To partially answer my own question, I need to put {data:{}} around the request.

Now I get a different error message:

{"errors":[{"message":"workspace: Not a valid GID type: \"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"}]}

GID are string, maybe you are passing a number?

I did think of that, but as you can see from the original posting:

"workspace":{"gid":"9814050864025"}

Where you’re specifying related objects like projects. assignee, workspace - don’t try to put it in as an object, just put the gid value itself. In other words, build your json to look like this (I edited this json by hand and didn’t test it explicitly - it should be correct but anyway you’ll get the idea):

{"projects":["1113266773900829"],"assignee":"9814050857874","name":"Task 1","notes":"Added we assume to Test Project","workspace":"9814050864025"}

4 Likes

hi All,
we encountered an issue with API

error April 7th at 6:12 { “message”: “{"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. Usually waiting and then retrying the request once fixes this. If the issue is persistent, please contact api-support@asana.com and include the error phrase from this response.","phrase":"11 drunk salmon roar angrily"}]}” }

Asana support suggested to post this here.

Looking forward to your support.
Best,
Alessandra

Hi @Alessandra2,

Actually as users we don’t have access to or knowledge about the error phrases. You should send that error message to api-support@asana.com and ask them to look and see what the error source is based on the phrase.

2 Likes