{ ... }).don-6ren">
gpt4 book ai didi

react-native fetch 和基本身份验证

转载 作者:行者123 更新时间:2023-12-03 13:22:38 25 4
gpt4 key购买 nike

我尝试通过以下语法使用 fetch:

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

相同的 url 在 CURL 中有效,但在 React Native 中返回 401。

有任何想法吗?

谢谢,
保罗

最佳答案

我发现这种格式https://user:password@url在 CURL 和节点中运行良好,但不适用于 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();
`

关于react-native fetch 和基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34815853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com