Home > Software engineering >  Facing encoding problem in angular in url
Facing encoding problem in angular in url

Time:09-29

chrome is encoding the url. anyone know any solution

CodePudding user response:

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.

CodePudding user response:

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