I'm trying to work with the Asana API as I learn React and Redux. I've been able to get data from the Asana API using fetch() just fine so far, but I'm having trouble posting a task.
Here is the code I'm using:
const options = (type, data) => {
const defaultHeaders = {
'Authorization': `Bearer ${apiKey}`,
'Asana-Fast-Api': 'true',
}
switch(type) {
case 'get':
return {
headers: defaultHeaders,
}
case 'post':
const body = JSON.stringify(data)
console.log(body);
return {
method: 'POST',
headers: defaultHeaders,
contentType: 'application/json',
body: body,
}
default:
return {
headers: defaultHeaders,
}
}
};
const asanaUrl = (props) => {
const numOfProps = props.length;
switch (numOfProps) {
case 3:
return `https://app.asana.com/api/1.0/${props[0]}/${props[1]}?${props[2]}`
case 2:
return `https://app.asana.com/api/1.0/${props[0]}?${props[1]}`
case 1:
return `https://app.asana.com/api/1.0/${props[0]}`
default:
return console.log(props)
}
}
export const asanaPost = (props, data) => {
return fetch(asanaUrl(props), options('post', data))
.then(response => response.json() )
}
In the console, I see the return from the console.log which shows the JSON I'm sending into my body key:
{"assignee":22800770039251,"name":"test","notes":"test"}
and the following error
Failed to load resource: the server responded with a status of 400 (Bad Request)
The URL appears to be correct: https://app.asana.com/api/1.0/tasks?workspace=31542879721131
The error message is:
"Must specify exactly one of project, tag, or assignee + workspace"
It doesn't seem to matter what fields I include in the body (including a project resulted in the same error), which makes me suspect that something else is afoot and the Asana API isn't getting a hold of the body or isn't able to interpret it with how I've set things up.
Thanks for helping me out with this!