简体   繁体   English

如何在 NestJS 中使用基本身份验证?

[英]How to use the basic authentication in NestJS?

So I'm trying to do an API call, and I have to use basic authentication.所以我试图做一个 API 调用,我必须使用基本身份验证。

The codes are like these:代码如下:

const headersRequest = {
    'Content-Type'  : 'application/json',
    'Authorization' : `Basic ${this.authToken}`
}

let response = this.httpService.get(url, {headers: headersRequest});

The question is the above get() function call correct?问题是上面的get() function 调用正确吗?

Because the output of:因为 output 的:

console.log(response)

is:是:

Observable { _isScalar: false, _subscribe: [Function (anonymous)] }

and not the response object which I wanted.而不是我想要的响应 object。

I have tried to explore HttpService get , but couldn't find any detailed documentation related to this basic authentication.我曾尝试探索HttpService get ,但找不到与此基本身份验证相关的任何详细文档。

PS: I'm a PHP developer and just learned NestJS in the last couple of days. PS:我是一名 PHP 开发人员,最近几天刚刚学习了 NestJS。

The easiest way to get the proper result from the observable is to to just convert it to a promise in this case.在这种情况下,从 observable 获得正确结果的最简单方法是将其转换为 promise。

const response = await this.httpService.get(url, {headers: headersRequest}).toPromise()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM