我尝试使用fetch与这种语法:
- fetch("https://user:password@url",{
- ...
- }).then((response) => {
- ...
- }).done();
相同的网址在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();
- `