首页 > 解决方案 > 如何在 NestJS 中使用基本身份验证?

问题描述

所以我正在尝试进行 API 调用,我必须使用基本身份验证。

代码如下:

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

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

问题是上面的get()函数调用正确吗?

因为输出:

console.log(response)

是 :

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

而不是我想要的响应对象。

我试图探索HttpService get,但找不到与此基本身份验证相关的任何详细文档。

PS:我是一名 PHP 开发人员,最近几天才学习了 NestJS。

标签: getnestjsbasic-authenticationhttpservice

解决方案


从 observable 获得正确结果的最简单方法是在这种情况下将其转换为 Promise。

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


推荐阅读