Eldar
Eldar

Reputation: 330

Angular 4 http get with query parameters

The first block is working as expected

getQuotes(): Observable<Quote[]> {
    return this.http.get(this.url)
        .map((res: Response) => res.json())
        .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}

Now I am tring to add query params to this.url and the url have not changed

getQuotes2(): Observable<Quote[]> {
    let myParams  = new URLSearchParams();
    myParams.append('author', 'authorName');
    myParams.append('catid', '123');
    let options = new RequestOptions({ params: myParams });

    return this.http.get(this.url, options )
        .map((res: Response) => res.json())
        .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}

I have checked in devtools. Caching is switched off. I have tried { search: myParams }, with RequestOptions and return this.http.get(this.url, { params: myParams } ) Where I am not looking i see string concatination. These parameters are optinal and I append them on condition.

Upvotes: 4

Views: 3228

Answers (1)

Vova Bilyachat
Vova Bilyachat

Reputation: 19514

Have you imported params ?

import { URLSearchParams } from '@angular/http';

Upvotes: 6

Related Questions