15

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

1
  • 1
    have you tried passing an array? Commented Jan 23, 2015 at 17:45

2 Answers 2

26

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
});
Sign up to request clarification or add additional context in comments.

3 Comments

I had a code error I just fixed, fyi. If you are copying look my answer over.
FYI: $http returns a promise.
As per your answer it will be someParmName=value1&someParmName=value2 but can it be changed to make query like someParmName=value1,value2 ?
4

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});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.