angular 6 个 httpclient 在 url 中传递凭据

angular 6 httpclient pass credentials in url

我试图通过传递 URL 中的凭据从 angular 6 项目向 CouchDB 发出 HTTP GET 请求。但我总是收到 401 错误 "You are not authorized to access this db."。

这里是代码:

var url = "http://user:password@localhost:1234/mydatabase";
 this.http.get(url)
          .subscribe(
              data => console.log(data),
              error => console.log(error)
          );

我也用 {'withCredentials': true} 选项尝试过,但得到了同样的错误。

Angular版本:6.1.0,使用HttpClient

试试下面的代码。根据记忆,您不能将 username:password 与 HttpClient

一起使用
const headers = new HttpHeaders(
{
     'Authorization':'Basic '+btoa(`${username}:${password}`)
});

var url = "http://localhost:1234/mydatabase";
 this.http.get(url, {headers})
      .subscribe(
          data => console.log(data),
          error => console.log(error)
      );