react-native fetch and basic authentication

后端 未结 2 2038
一整个雨季
一整个雨季 2021-02-05 09:33

I tried to use fetch with this syntax:

fetch(\"https://user:password@url\", {
   ...
}).then((response) => {
   ...
}).done();

The same url

2条回答
  •  醉梦人生
    2021-02-05 10:30

    I found that this format https://user:password@url works well in CURL and node but not with fetch.

    I had to use base-64 npm module and pass through a Headers object.

    // 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();
    `
    

提交回复
热议问题