0

I am trying to make multiple params value for single value in Angular get request, but it returning array for the value here is my code.

let params = new HttpParams;
let prefectures = [01, 02] //dynamic array of numbers
for(let pre of prefectures){
  params = params.append('prefecturesCode', pre);
}

here is the request code

return this.http
  .get(this.replaceBaseUrl(url), {
    headers,
    params: params
  })

I am getting this url

http://localhost:3003/endpoint?prefecturesCode=02,03

But what I need is like this

http://localhost:3003/endpoint?prefecturesCode=02&prefecturesCode=03

What I am doing wrong?

1
  • I think the expected URL is not a standard, you can stick to default behavior and split the prefecturesCode value with a comma in your API. Otherwise, if it is a requirement then you may loop through the dynamic array, create and update the string variable, and append it to the URL. Commented Aug 30, 2020 at 17:22

1 Answer 1

0

I think if you need to pass a list of value in a query parameter the right way is

http://localhost:3003/endpoint?prefecturesCode=02,03

Think, for example, in node.js, when Express parse the query parameters in request it store in the params object which have a property with the name of the parameter. Thereforce you will get only the value 03 or an error if you send the same parameter twice.

Hope this can help you.

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

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.