get - 如何在 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。
解决方案
从 observable 获得正确结果的最简单方法是在这种情况下将其转换为 Promise。
const response = await this.httpService.get(url, {headers: headersRequest}).toPromise()
推荐阅读
- ruby-on-rails - activerecord 复杂连接
- java - 将 gradle run 使用 @NotThreadSafe 注释的测试一次作为单个测试进程运行
- wix - 如何在文件上保留“修改日期”
- java - 如何将矩阵的不同行传递给线程池。[ 爪哇 ]
- rest - 让下游服务访问在上游服务中写入 API 是否被认为是糟糕的架构?
- html - 调整大小时导航栏背景图像不展开
- polymer-3.x - LitElement 中的 Polymer.importPath
- php - 如何根据 XML 文件的搜索返回完整的子节点集
- c# - 匹配前缀后由空格分隔的字符序列
- sql - 使用基于同一表中的 varchar 字段的序列号更新 Oracle 表列