Skip to content

Instantly share code, notes, and snippets.

@omelsoft
Forked from P1xt/wikipedia.service.ts
Created April 12, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omelsoft/77473f8ef62c9fbaa38eec0aab5ea2f3 to your computer and use it in GitHub Desktop.
Save omelsoft/77473f8ef62c9fbaa38eec0aab5ea2f3 to your computer and use it in GitHub Desktop.
Setting headers on an Angular 2 http get request
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
@Injectable()
export class GetWikipediaService {
constructor(private http: Http) { }
searchWiki = (query) => {
let headers = new Headers();
headers.append('Api-User-Agent', 'Example/1.0');
let apiUrl: string = 'https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&generator=search&prop=extracts|info&inprop=url&exintro&explaintext&exsentences=1&exlimit=max&gsrsearch='+query;
return this.http
.get(apiUrl, headers)
.map(response => response.json());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment