0

How can I add a custom header to http request using angular5? I have tried something like this.

        import {Headers} from 'angular2/http';
        var headers = new Headers();
        headers.append(headerName, value);

        // HTTP POST using these headers
         this.http.post(url, data, {
         headers: headers
          })
      // do something with the response
3

1 Answer 1

0

You can refer below example for Custom header:

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


@Injectable()
export class AbcService {

  constructor(private http: Http) { }

  search = (query) => {
    let headers = new Headers();
    headers.append('Api-User-Agent', 'Example/1.0');
    let apiUrl: string = 'https://en.wikipedia.org/w/api.phpgsrsearch='+query;

return this.http
        .get(apiUrl, headers)
        .map(response => response.json());
  }
}
Sign up to request clarification or add additional context in comments.

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.