1

Trying to add custom attribute to HttpHeader request but it doesn't seem to be sent .

  getUserInfo(){
    this.headers.append('CustomerId', 'ad34e1b2'); 
    return this._http.get(this.baseUrl+"getInfo",{ headers:this.headers });
  }

2 Answers 2

2

If you check the post() method parameters of HttpClient module you will able to see the acceptable parameters as below:

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

 /**
 * Construct a POST request which interprets the body as JSON and returns the full response.
 *
 * @return an `Observable` of the `HttpResponse` for the request, with a body type of `Object`.
 */
post(url: string, body: any | null, options: {
    headers?: HttpHeaders | {
        [header: string]: string | string[];
    };
    observe: 'response';
    params?: HttpParams | {
        [param: string]: string | string[];
    };
    reportProgress?: boolean;
    responseType?: 'json';
    withCredentials?: boolean;
}): Observable<HttpResponse<Object>>;

which means the function accepts url, body and options as a main acceptable parameters. you can specify the custom headers by creating a function which will accept the header name and value. like this in case of single custom header in case of multiple headers you are free to write your own version of function.

private setHeaders(customHeader: string, value: string): HttpHeaders {
const headers: HttpHeaders = new HttpHeaders({
  customHeader: value,
});
return headers;
}

and lately don't forget to import these libraries.

import { HttpClient, HttpHeaders } from '@angular/common/http';
Sign up to request clarification or add additional context in comments.

Comments

1

you need to import new HttpHeaders

import { HttpClient, HttpHeaders } from '@angular/common/http';

create an instance of it in your function

let headers = new HttpHeaders();
 headers.append('CustomerId', 'ad34e1b2');

or creating multiple you can add like

let headers = new HttpHeaders(
{'abc':'22',
'abc2':'111',
'abc4':'444'}
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.