如何在Node JS中使用基本身份验证在Node-Fetch模块中传递数据

问题描述

有人可以提供此代码帮助吗?当我通过一个具有基本身份验证状态的“发布”请求时,身份验证状态为200,但没有任何响应。在没有身份验证的情况下,相同的代码有效

let username = 'username';
let password = 'password';
let headers = new Headers();
headers.set('Authorization','Basic ' + base64.encode(username + ":" + password));

fetch('https://sandBox.transactions.com',{
    method: 'post',body: JSON.stringify(body),headers: headers
  })
  .then(function(response) {
    return response.json();
  }).then(function(json) {
    console.log(json);

  }).catch(function(err) {
    console.log('There is an error ');
    res.status(500).send('There was an error while fetching');
  });

解决方法

如果状态为200,则表示一切正常。如果没有得到任何数据作为回报,则可能意味着服务器没有适合您的数据。

来自page view

除了对CONNECT的响应外,200响应始终具有有效负载,尽管原始服务器可以生成零长度的有效负载主体。