首页 > 解决方案 > 阅读 API 文档

问题描述

我正在尝试重新创建这个创建一个新的卡API,但是基于文档,我不确定我应该如何传递所需的数据,它是否应该在查询参数中(req.params 来收集数据),例如 /1/cards?name=cardName&desc=cardDescription,还是应该在 req.body 内?

标签: node.jsresthttp

解决方案


该文档显示了该 API 的大量示例。 其中一个示例甚至是为 nodejs 编写的。看起来很清楚,选项是查询参数,请求本身没有正文数据。

// This code sample uses the 'node-fetch' library:
// https://www.npmjs.com/package/node-fetch
const fetch = require('node-fetch');

fetch('https://api.trello.com/1/cards?idList=5abbe4b7ddc1b351ef961414', {
  method: 'POST',
  headers: {
    'Accept': 'application/json'
  }
})
  .then(response => {
    console.log(
      `Response: ${response.status} ${response.statusText}`
    );
    return response.text();
  })
  .then(text => console.log(text))
  .catch(err => console.error(err));

同样,CURL 示例也清楚地说明了这一点:

curl --request POST \
  --url 'https://api.trello.com/1/cards?idList=5abbe4b7ddc1b351ef961414' \
  --header 'Accept: application/json'

推荐阅读