如何通过Axios nodejs正确发送承载令牌Auth头?

我从axios POST中收到以下错误:

 import axios, { AxiosInstance } from 'axios';
 ....
 let headers = {
  "Authorization": "Bearer exxxx",
  "Content-Type": "application/json"
 }
 .....
 this.axiosInst = axios.create({
            baseURL: this.baseUrl
        });
 ...
 await this.axiosInst.post(endpoint, data, { headers });

这是错误:

{ Error: Request failed with status code 401
    at createError ([...]node_modules/axios/lib/core/createError.js:16:15)
    at settle ([...]node_modules/axios/lib/core/settle.js:17:12)
    at IncomingMessage.handleStreamEnd ([...]node_modules/axios/lib/adapters/http.js:236:11)
    at IncomingMessage.emit (events.js:203:15)
    at IncomingMessage.EventEmitter.emit (domain.js:448:20)
    at endReadableNT (_stream_readable.js:1145:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  config:

  { url: 'https://myendpoint.io/prod/create',
     method: 'post',
     data:
      '{"entryId":"xxxx","Sku":"xxx-24HK3TY9", .....}',
     headers:
      { Accept: 'application/json, text/plain, */*',
        'Content-Type': 'application/json',
        Authorization:
         'Bearer eyxxxxx',
        'User-Agent': 'axios/0.19.2',
        'Content-Length': 4341 
      }
  }

因此,我得到401未经授权,好像我的载体令牌无效或(我认为)没有正确注入Axios帖子中

我相信令牌是可以的,因为我可以通过邮递员从错误配置对象(URL,数据和授权承载令牌)进行复制和粘贴来成功执行POST

评论