【发布时间】:2022-02-15 14:45:49
【问题描述】:
我正在根据本指南编码:https://angular.io/guide/http#configuring-other-parts-of-the-request。
我的代码如下:
loadMenuOptions(): void {
console.log('this.currentApiKey |' + this.currentApiKey);
let header = new HttpHeaders();
header = header.set('api-key', this.currentApiKey);
this.commonService. getMenuOptions(MENU_OPTIONS, header).subscribe(respuesta => this.setMenuOptions(respuesta));
}
以下代码是我将这个对象发送到服务器时:
getMenuOptions(endPoint: string, header: HttpHeaders): Observable<OptionsResponse> {
console.log('header:' + JSON.stringify(header));
return this.http.get<OptionsResponse>(endPoint, header)
.pipe(
tap(res => this.log('getMenuOptions | status | ' + res.header.status)),
catchError(this.handleError('getMenuOptions', null)));
}
JSON.stringify 显示此值:
header:{"normalizedNames":[],"lazyUpdate":[{"name":"api-key","value":"JEFE_HHHABBBJJJXXX","op":"s"}],"headers":[] ,"lazyInit":{"normalizedNames":[],"lazyUpdate":null,"headers":[]}}
但服务器没有收到“api-key”值。
我用相同的值执行了 POSTMAN,服务器正确接收到了 'api-key' 值。
我做错了什么?
更新
此快照表示第一次调用 'getMenuOptions' 方法:first call to the server
此截图属于对服务器的第二次调用:
正如您在第二次调用的第二部分看到的那样,包含“api-key”值的标头在“lazyUpdate”对象中发送。
【问题讨论】:
-
HttpHeaders是不可变对象,所以你必须参考这个answer -
@Aravind,如果你看到我的代码,你会看到我创建了一个新的标头对象。
-
能否添加浏览器网络开发工具中记录的请求/响应标头?
-
@Jota.Toledo,我更新了我的帖子。 :)
-
请将您的源代码添加到您的问题中,而不是图片中
标签: angular http-headers angular-httpclient