How to set Headers using append or set does not work? in Angular 2

Discussão em 'Angular' iniciado por Shamsher, Outubro 31, 2024.

  1. Shamsher

    Shamsher Guest

    I am using Angular 2.4.8. I want to append custom headers to each request. I am using BaseRequestOptions to define custom headers and adding it to app providers. Following is the code.

    import { BaseRequestOptions, Headers } from '@angular/http';
    import { Injectable } from '@angular/core';

    @Injectable()
    export class AppBaseRequestOptions extends BaseRequestOptions {
    public merge(headers: Headers) {
    headers.append('Content-type', 'application/json');
    headers.append('My-Custom-Header', 'My-Custom-Header-Value');
    return super.merge(headers);
    }
    }


    Provider is as follows:

    providers: [
    { provide: BaseRequestOptions, useClass: AppBaseRequestOptions }
    ]


    I have tried headers.append and headers.set but both throws

    Cannot read property 'append' of null
    Cannot read property 'set' of null

    Continue reading...

Compartilhe esta Página