我正在尝试在 NestJS 中发出 Http 请求
因为它的灵感来自 angular,所以我 jave 附加了我的标题
import { Injectable, HttpService} from '@nestjs/common';
...
const headersRequest = new Headers();
headersRequest.append('Content-Type', 'application/json');
headersRequest.append('Authorization', `Basic ${encodeToken}`);
然后调用api
const result = await this.httpService.post(apiUrl, newDevice, { headers: headersRequest });
我得到一个错误
ReferenceError: Headers is not defined
当我将 Headers
导入时
我在 VScode 中收到警告消息
Only a void function can be called with the 'new' keyword.
最佳答案
NestJS 使用 axios在引擎盖下发出 http 请求,请查看其请求配置文档:
https://github.com/axios/axios#request-config
貌似没有headers的接口(interface),只传一个普通的JS字典对象:
const headersRequest = {
'Content-Type': 'application/json', // afaik this one is not needed
'Authorization': `Basic ${encodeToken}`,
};
const result = await this.httpService.post(apiUrl, newDevice, { headers: headersRequest });
关于javascript - 在 NestJS 中添加 header HttpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52972616/