react-native fetch和basic authentication

Pau*_*aul 18 react-native

我尝试使用这种语法的fetch:

fetch("https://user:password@url", {
   ...
}).then((response) => {
   ...
}).done();
Run Code Online (Sandbox Code Playgroud)

相同的URL在CURL中起作用,但在React Native中返回401.

有任何想法吗?

谢谢,保罗

Pau*_*aul 34

我发现这种格式https://user:password@url在CURL和节点中运行良好,但在fetch中运行不正常.

我不得不使用base-64npm模块并传递Headers对象.

// https://www.npmjs.com/package/base-64
const base64 = require('base-64');

...

var headers = new Headers();
headers.append("Authorization", "Basic " + base64.encode("user:password"));

fetch("https://url", {
    headers: headers
  })
  .then((response) => { ... })
  .done();
`
Run Code Online (Sandbox Code Playgroud)