【发布时间】:2017-11-04 06:36:36
【问题描述】:
在学习 React 和 Redux 时,我正在尝试使用 Asana API。到目前为止,我已经能够使用 fetch() 从 Asana API 获取数据,但我无法发布任务。
这是我正在使用的代码:
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() )
}
在控制台中,我看到了来自 console.log 的返回,它显示了我发送到我的正文键中的 JSON:
{"assignee":22800770039251,"name":"test","notes":"test"}
还有以下错误
Failed to load resource: the server responded with a status of 400 (Bad Request)
网址似乎正确:https://app.asana.com/api/1.0/tasks?workspace=31542879721131
错误信息是:
"Must specify exactly one of project, tag, or assignee + workspace"
我在正文中包含哪些字段似乎并不重要(包括导致相同错误的项目),这让我怀疑正在发生其他事情并且 Asana API 没有掌握正文或者无法用我的设置方式来解释它。
感谢您帮助我解决这个问题!
【问题讨论】: