英文:
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}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论