Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple same-name query parameters in Angular $http

All

What I want to do is Adding multiple parameters with same name to request URL with AngularJS $http, making the URL sent like http://localhost:8080/?value=1&value=2.....

This post also described the similar situation: Sending HTTP request with multiple parameters having same name

Could any one help with in AngularJS $http?

Thanks

like image 522
Kuan Avatar asked Jan 23 '15 17:01

Kuan


People also ask

How do you pass multiple parameters in HTTP GET request in Angular 8?

Passing multiple parameters to Http get requestBy using HttpParams. append() we can pass multiple parameters to HttpClient. get() method. We have to pass page & per_page parameters to the list of users API.

How do I pass multiple query parameters in REST URL?

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.

How do I add multiple parameters to a URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".


2 Answers

You need to use an array like this:

 var params = {};
 params.someParmName = ['value1', 'value2'];

This will send value1 and value2 with the same querystring key 'someParmName'.

Here is an example GET request:

 var parameters = {};
 parameters.someParmName = ['value1', 'value2'];

 $http({
    url: 'api_url', 
    method: "GET",
    params: parameters
});
like image 116
nweg Avatar answered Sep 28 '22 07:09

nweg


You call also use URLSearchParams for the parameters. For example:

let params = new URLSearchParams();

params.append('someParmName', 'value1');
params.append('someParmName', 'value2');

// the following statement will form this url: ?someParmName=value1&someParmName=value2
http.get(api_url, {search: params});

Or

let params = new URLSearchParams();

params.set('someParmName', ['value1', 'value2'];);

// the following statement will form this url: ?someParmName=value1,value2
http.get(api_url, {search: params});
like image 45
arman Avatar answered Sep 28 '22 07:09

arman