5

In Angular 4's HTTP package ('@angular/http'), a URLSearchParams object can be passed in the get request. When assigning the parameters object in the request method, what is the difference between using search and params as the attribute to pass the value into?

For example, what is the difference between the following two pieces of code:

let params = new URLSearchParams();
params.set('param1', 'xyz');
this.http.get('url', { search: params });

and

let params = new URLSearchParams();
params.set('param1', 'xyz');
this.http.get('url', { params: params });

Many thanks.

2 Answers 2

12

Search is deprecated since 4.0 and params is preferred way of passing query params.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I appreciate the answer, the documentation has been in constant flux and it is also very lacking in details. Thanks again.
-1

You can use HttpParams class!

Check this snippet:

import { HttpParams } from '@angular/common/http';

private setParams(parameters): HttpParams {
    let params = new HttpParams();
    let keys = Object.keys(parameters);

    keys.forEach( (key) => {
        params = params.append(key, parameters[key].toString()); 
    });

    return params;
}

And in your request:

let params = this.setParams({});
http.get(your_url, {params: params});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.