Adding a HTTP header to the Angular HttpClient doesn't send the header, why?

前端 未结 9 1120
孤街浪徒
孤街浪徒 2020-11-22 05:45

Here is my code:

import { HttpClient, HttpErrorResponse, HttpHeaders } from \'@angular/common/http\';
9条回答
  •  深忆病人
    2020-11-22 06:30

    I struggled with this for a long time. I am using Angular 6 and I found that

    let headers = new HttpHeaders();
    headers = headers.append('key', 'value');
    

    did not work. But what did work was

    let headers = new HttpHeaders().append('key', 'value');
    

    did, which makes sense when you realize they are immutable. So having created a header you can't add to it. I haven't tried it, but I suspect

    let headers = new HttpHeaders();
    let headers1 = headers.append('key', 'value');
    

    would work too.

提交回复
热议问题