javascript 错误401(未授权)

nmpmafwu  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(153)

我试图在 node.js 上对url发出 POST 请求,但总是得到错误401(未经授权)。

const response = await fetch('url', {
  method: 'POST',
  mode: 'no-cors',
  headers: {
    "Content-Type": "text/html; charset=utf-8",
    "Authorization": 'Basic ' + Buffer.from('username' + ":" + "password", 'base64').toString('base64')
  },
  body: ''
});

我尝试使用 btoa,但是 node.js 不支持...

headers.set('Authorization', 'Basic ' + btoa(username + ":" + password));
yeotifhr

yeotifhr1#

试试这个:

let loginData = "username" + ":" + "password";
let encodedData = new Buffer(loginData);
let base64data = encodedData.toString('base64');

相关问题