react-native fetch 和基本身份验证 [英] react-native fetch and basic authentication

查看:51
本文介绍了react-native fetch 和基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I tried to use fetch with this syntax:

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

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

The same url works in CURL but returns 401 in React Native.

有什么想法吗?

谢谢,保罗

推荐答案

我发现这种格式 https://user:password@url 在 CURL 和 node 中运行良好,但不适用于 fetch.

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

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

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

这篇关于react-native fetch 和基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆