Angular 2 http.get with params
You can leverage the URLSearchParams
class for this:
getTranslation(lang: string): Observable<any> {
let params = new URLSearchParams();
params.set('param1', 'value1');
return this.http.get(routes.Localization.Get, { search: params });
}
This will result to an URL like this (parameters are added in the query string): http://...?param1=value1
.
See the documentation of this class:
- https://angular.io/docs/ts/latest/api/http/index/URLSearchParams-class.html
It now providers support for encoding / decoding parameters.
This is pretty simple - you can define your URLSearchParams
and pass them in the second parameter of the http.get
method:
import { URLSearchParams } from '@angular/http'
let params: URLSearchParams = new URLSearchParams();
params.set('param1', 'someValue');
params.set('param2', 'someValue');
return this.http.get(routes.Localization.Get, { search: params });