在 node.js 中正确使用 data/body 和 axios

Correct usage of data/body with axios in node.js

我在尝试此代码时遇到错误 'NOT_FOUND',错误代码为 404:

    axios.get(`https://api.exchange.bitpanda.com/public/v1/account/deposit/crypto`, {
      headers: {
        'content-type': 'application/json',
        'Authorization': 'Bearer api_key'
      },
      data: {
        "currency": "BTC"
      }
    })
    .then(function (response) {
      console.log(response.data)
    })
    .catch(function (error) {
      console.log(error.response.data);
      console.log(error.response.status);
    })

由于某些原因 node.js

中没有示例

Picture of documentation

Bitpanda api documentation

你得到 404,因为根据 documentation

,对 https://api.exchange.bitpanda.com/public/v1/account/deposit/crypto 的请求应该是 POST 而不是 GET
 axios.post(REST_OF_THE_CODE_HERE);

另外以不同的方式构建您的请求,阅读 axios 的工作原理 here

const headers = 
 {
    'content-type': 'application/json',
    'Authorization': 'Bearer api_key'
 }

axios.post(URL, payload, { headers: headers})
      .then(function (response) {
        console.log(response.data)
      })
     .catch(function (error) {
        console.log(error.response.data);
        console.log(error.response.status);
     })