0

How can I convert an object like that

{
    param1: "value1",
    param2: "value2",
    param3: ["value31" "value32"]    
}

into a querystring like that:

param1=value1&param2=value2&param3[]=value31&param3[]=value32

to be passed along an http.get reuquest?

2
  • This may help Commented Jul 14, 2017 at 11:08
  • well, I was actually looking for a sort of native Angular API to do that, rather than a custom parsing function. Also, I have no intention of using jquery! Commented Jul 14, 2017 at 11:12

1 Answer 1

1

I think this is something that you are looking for:

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

let someObject = {
  param1: "value1",
  param2: "value2",
  param3: ["value31" "value32"]    
}

let queryString = new URLSearchParams();

for (const key in someObject) {
  queryString.set(key, someObject[key]);
}

queryString.toString();

You can find out more here URLSearchParams API about the different options.

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

2 Comments

In this way what I get is: param1=value1&param2=value2&param3=value31,value32 which is different from what my remote service expects...
Then the only solution is doing your own custom parser.

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.