0

I am attempting to create a task in Asana using Perl. I am using the following modules:

  • WWW::Curl::Simple
  • JSON
  • HTTP::Request

Here is my code.

my %data = (
    "data" => {
        "workspace" => "##########", #$config->get('asana/workspace_id'),
        "name" => "system error",
        "assignee" => "me",
        "projects" => "##########",
    },
);
my @header = ('Authorization' => 'Bearer '.$personal_access_token));
my $curl = WWW::Curl::Simple->new();
my $uri = $config->get('asana/api_uri');
my $content = JSON->new->utf8->encode(\%data);
my $r = HTTP::Request->new(
    'POST',
    $uri,
    \@header,
    $content
);
my $res = $curl->request($r);

When I print the $content variable, it looks like this.

{"data":{"workspace":"##########","name":"CBC FZDS Billing - System Error"}}

When I print the $r variable as a string, it looks like this. ("personal access token" displays my the personal access token that I have provided.)

POST https://app.asana.com/api/1.0/tasks
Authorization: Bearer <personal access token>

{"data":{"workspace":"##########","name":"CBC FZDS Billing - System Error"}}

The result from $res->content is:

'{"errors":[{"message":"missing `workspace` field, and no `parent` or `projects` specified","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"}]}'

Any ideas why this is indicating that the workspace field is missing?

Will Reade
  • 23
  • 3
  • 1
    From the examples it looks like the top-level `data` key that you have is unnecessary: https://asana.com/developers/api-reference/tasks#create – Hunter McMillen Jul 12 '16 at 15:14
  • @HunterMcMillen: Thanks for your response. Here is the $content variable now. {"workspace":"#######","name":"System Error"} Here is the $r variable. POST https://app.asana.com/api/1.0/tasks Authorization: Bearer {"workspace":"#######","name":"System Error"} I get the same result from $res->content. Any other suggestions? – Will Reade Jul 13 '16 at 14:07

1 Answers1

0

It looks to me like you need to set the content-type header to "application/json" because you are sending your data as json.

To help you debug future issues, you can try our curl in the command line. In this particular case you'll notice that if you add -H "Content-Type: application/json" it works and if you leave it out it will give you the same error.

(works) curl -H "Authorization: Bearer ????" -X POST "https://app.asana.com/api/1.0/tasks" -d "{\"data\": { \"workspace\": ZZZZZZZ, \"name\": \"test\" }}" -H "Content-Type: application/json"

vs (doesn't work) curl -H "Authorization: Bearer ????" -X POST "https://app.asana.com/api/1.0/tasks" -d "{\"data\": { \"workspace\": ZZZZZZZ, \"name\": \"test\" }}"

Mark
  • 176
  • 4