【发布时间】:2016-04-21 07:54:18
【问题描述】:
我尝试通过以下语法使用 fetch:
fetch("https://user:password@url", {
...
}).then((response) => {
...
}).done();
相同的 url 在 CURL 中有效,但在 React Native 中返回 401。
有什么想法吗?
谢谢, 保罗
【问题讨论】:
标签: react-native
我尝试通过以下语法使用 fetch:
fetch("https://user:password@url", {
...
}).then((response) => {
...
}).done();
相同的 url 在 CURL 中有效,但在 React Native 中返回 401。
有什么想法吗?
谢谢, 保罗
【问题讨论】:
标签: react-native
我发现https://user:password@url 这种格式在 CURL 和 node 中效果很好,但在 fetch 中效果不佳。
我必须使用 base-64 npm 模块并通过 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();
`
【讨论】:
RNFetchBlob.base64.encode(authString)
base-64:npmjs.com/package/base-64
您可以使用 btoa() 而不是使用 base_64 模块。 btoa() 是Window 上的一个函数。
【讨论】: