chrome is encoding the url. anyone know any solution
2 Answers
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.
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
techguy
it is not working. in chrome, it is working same, it is working fine only in firefox
MGX
And
{ userId: [1] } ?techguy
I need [] character in key like userId[]
MGX
@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.
techguy
if forming the url : posts?userId=1 , I need posts?userId[]=1
|