react-native fetch and basic authentication

后端 未结 2 2037
一整个雨季
一整个雨季 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();
    `
    
    0 讨论(0)
  • 2021-02-05 10:31

    You could have used btoa() instead of using the base_64 module. btoa() is a function on the Window.

    0 讨论(0)
提交回复
热议问题