Axios API Post请求在Node.js中出现错误。

huangapple go评论77阅读模式
英文:

Error in Axios API Post request in Nodejs

问题

我正在尝试调用Paystack API。我正在使用node.js的Axios包。看起来无法正常工作。

以下是我的代码:

const response = await axios({
  method: 'post',
  url: 'https://api.paystack.co/transaction/initialize',
  data: paymentDetails,
  headers: {
    Authorization: 'mysecretKey',
    'content-type': 'application/json',
    'cache-control': 'no-cache'
  },
})
return res.json(response)

在Postman中,我只是按Paystack要求通过请求体提供了支付细节:

"id": 1,
"username": "kings",
"email": "emailaddress",
"amount": 100

我收到这个错误:AxiosError: Request failed with status code 401

英文:

I am trying to make an API call to Paystack API. I am using Axios package for node.js. I can't seems to get it to work.

Here is my code:

  const response = await axios({
    method: 'post',
    url:'https://api.paystack.co/transaction/initialize',
    data: paymentDetails,
    headers : {
        Authorization: 'mysecretKey',
          'content-type': 'application/json',
          'cache-control': 'no-cache'
      },
})
return res.json(response)

On postman, I simply provided the payment details via the body as requested by Paystack:

"id": 1,
"username": "kings",
"email": "emailaddress",
"amount": 100

I am getting this error: AxiosError: Request failed with status code 401

答案1

得分: 1

你可能将 Authorization 弄错了。

尝试这样做:

const response = await axios({
    method: 'post',
    url: 'https://api.paystack.co/transaction/initialize',
    data: paymentDetails,
    headers : {
        Authorization: `Bearer ${mysecretKey}`,
        'content-type': 'application/json',
        'cache-control': 'no-cache'
    },
})
return res.json(response)

注意 Authorization: Bearer {secret}

https://paystack.com/docs/api/#authentication

英文:

You probably have the Authorization wrong.

Try this:

const response = await axios({
    method: 'post',
    url:'https://api.paystack.co/transaction/initialize',
    data: paymentDetails,
    headers : {
        Authorization: `Bearer ${mysecretKey}`,
          'content-type': 'application/json',
          'cache-control': 'no-cache'
      },
})
return res.json(response)

Notice the Authorization: Bearer {secret}

https://paystack.com/docs/api/#authentication

huangapple
  • 本文由 发表于 2023年1月10日 19:04:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75069019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定