I am attempting to create a task in Asana using Perl. I am using the following modules:
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?
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\" }}"