HttpHeader 未正确发送 [英] HttpHeader isn't correctly sent
问题描述
我根据本指南进行编码:https://angular.io/guide/http#configuring-other-parts-of-the-request.
我的代码如下:
loadMenuOptions(): void {console.log('this.currentApiKey |' + this.currentApiKey);让 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{console.log('header:' + JSON.stringify(header));return this.http.get(endPoint, header).管道(tap(res => this.log('getMenuOptions | status | ' + res.header.status)),catchError(this.handleError('getMenuOptions', null)));}
<块引用>
JSON.stringify 显示此值:
header:{"normalizedNames":[],"lazyUpdate":[{"name":"api-key","value":"JEFE_HHHABBBJJXXX","op":"s"}],"headers":[],"lazyInit":{"normalizedNames":[],"lazyUpdate":null,"headers":[]}}
但服务器没有收到api-key"值.
我使用相同的值执行了 POSTMAN,服务器正确接收到了 'api-key' 值.
我做错了什么?
更新
此快照代表第一次调用 'getMenuOptions' 方法:第一次调用服务器
此截图属于第二次调用服务器:
正如您在第二次调用的第二部分所看到的,包含api-key"值的标头被发送到lazyUpdate"对象中.
问题在于您的 getMenuOptions
方法的实现.您不尊重 HttpClient
中 post
方法的定义.
应该是这样的:
http.post('/api/items/add', body, {标头:新 HttpHeaders().set('Authorization', 'my-auth-token'),}).
地点:
第一个论点:端点第二:请求体第三个:带有请求配置的对象
目前您将 headers 对象作为第二个参数传递并且没有提供配置对象(第三个参数),因此您的请求没有按预期运行是很自然的.
I am coding according to this guide: https://angular.io/guide/http#configuring-other-parts-of-the-request.
My code is the following:
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));
}
The following code is when I send this object to the server:
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 shows this value:
header:{"normalizedNames":[],"lazyUpdate":[{"name":"api-key","value":"JEFE_HHHABBBJJJXXX","op":"s"}],"headers":[],"lazyInit":{"normalizedNames":[],"lazyUpdate":null,"headers":[]}}
but the server is not receiving the 'api-key' value.
I executed POSTMAN with the same value and the server correctly received the 'api-key' value.
What am I doing wrong?
UPDATE
This snapshot represents the first time that is invoked the 'getMenuOptions' method: first call to the server
This screenshot belongs to the second call to the server:
As you are seeing at the 2nd part of the 2nd call, the header which contains the 'api-key' value is sent inside a 'lazyUpdate' object.
The problem is in the implementation of your getMenuOptions
method.
Your are not respecting the definition of the post
method from the HttpClient
.
It should be like this:
http
.post('/api/items/add', body, {
headers: new HttpHeaders().set('Authorization', 'my-auth-token'),
}).
Where:
1st arguement: endpoint
2nd: request body
3rd: an object with the request config
Currently you are passing the headers object as 2nd argument and giving no configuration object (3rd argument), so its natural that your request isn't behaving as expected.
这篇关于HttpHeader 未正确发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!