0

chrome is encoding the url. anyone know any solution

2 Answers 2

1

After chatting in the dedicated room you explained to me your need and here is the final solution, which is the simplest you can find :

    const params = { 'userId[]': [1, 2, 3] };

    const paramStr = Object.entries(params)
      .map(([key, value]) => `${key}=${value.join(',')}`)
      .join('&');

    this.http.get('http://www.test.com?' + paramStr).subscribe();

Working stackblitz (Open the network dev tools)

Other solutions involve rewriting an encoder and providing it to the http module. I'm not a fan of that, when you can just use a one liner to do the same thing.

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

3 Comments

please save your stackblitz. not able to see code
Sorry, edited it
please check chat
0
export class AppComponent {
  codec = new HttpUrlEncodingCodec();
  name = 'Angular';
  baseUrl = 'https://jsonplaceholder.typicode.com/';

  constructor(private http: HttpClient) {
    const params = { 'userId[]': 1 };
    this.http
      .get(this.baseUrl + 'posts', { params })
      .subscribe((data) => {
        this.name = this.name.concat(data[0].title);
      });
  }
}

8 Comments

it is not working. in chrome, it is working same, it is working fine only in firefox
And { userId: [1] } ?
I need [] character in key like userId[]
@techguy please try, see the result. I would explain the syntax to you right now, but if it does not work, there's no point.
if forming the url : posts?userId=1 , I need posts?userId[]=1
|

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.