Angular HttpHeader未正确发送

Angular HttpHeader未正确发送,angular,http-headers,angular-httpclient,Angular,Http Headers,Angular Httpclient,我正在根据以下指南进行编码: 我的代码如下: loadMenuOptions(): void { console.log('this.currentApiKey |' + this.currentApiKey); let header = new HttpHeaders(); header = header.set('api-key', this.currentApiKey); this.commonService. getMenuOptions(MENU_O

我正在根据以下指南进行编码:

我的代码如下:

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显示此值: 标头:{normalizedNames:[],lazyUpdate:[{name:api键,值:JEFE_hhhabbjjjxxx,op:s}],标头:[],lazyInit:{normalizedNames:[],lazyUpdate:null,标头:[]}

但服务器未接收到“api密钥”值

我用相同的值执行了POSTMAN,服务器正确地收到了“api key”值

我做错了什么

更新

此快照表示第一次调用“getMenuOptions”方法:

此屏幕截图属于对服务器的第二次调用:

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)));
  }

正如您在第二次调用的第二部分所看到的,包含“api key”值的头在“lazyUpdate”对象中发送。

问题在于getMenuOptions方法的实现。 您不遵守来自HttpClient的post方法的定义

应该是这样的:

http
  .post('/api/items/add', body, {
    headers: new HttpHeaders().set('Authorization', 'my-auth-token'),
  }).
其中:

1st arguement: endpoint
2nd: request body
3rd: an object with the request config

当前,您正在将headers对象作为第二个参数传递,并且没有给configuration object第三个参数,因此请求的行为不符合预期是很自然的。

HttpHeaders是不可变的对象,因此您必须引用此@Aravind,如果你看到我的代码,你会看到我创建了一个新的头对象。你能添加浏览器的网络开发工具中记录的请求/响应头吗?@Jota.Toledo,我更新了我的帖子:请在你的问题中添加你的源代码,而不是图片也许不是一个“post”方法,因为我没有向服务器发送任何正文,而是一个“get”方法。我试试这个。谢谢,@Jota.Toledo