-2

I want to use array as a parameter of get method of Angular. I use codes like below.

import { Http } from '@angular/http'; 
constructor(private http: Http) {}
this.http.get('http://localhost:5000/test',{
 params: {user}
})

user is defined as {id: number=101, name: string='john'}

and I expect to generate URL

http://localhost:5000/test?id=101&name=john

However, generated URL is

http://localhost:5000/test?user=%7B%id%22:%22YV1AMbiy%22,%22name%22:%22345678%22

kaz0621
  • 155
  • 1
  • 11

4 Answers4

1

You can use

let params = new HttpParams()
    .set('id', 101)
    .set('name','john');

 return this._HttpClient.get(URL, { params: params })
Suresh Kumar Ariya
  • 9,516
  • 1
  • 18
  • 27
0

Remove {} from params definition of get method

import { Http } from '@angular/http'; 
constructor(private http: Http) {}
this.http.get('http://localhost:5000/test', params: user )
callback
  • 3,981
  • 1
  • 31
  • 55
xcesco
  • 4,690
  • 4
  • 34
  • 65
0

Instead of doing params: {user} it should be params: user. Otherwise, it will be transpiled as params: { 'user': user }

callback
  • 3,981
  • 1
  • 31
  • 55
0

You can use JSON.stringify in Angular

this.http.get(
    `http://localhost:5000/test`,
    {
        params: { user: JSON.stringify(user) },
    },
);

And in your backend you can use:

const user = JSON.parse(req.query.user)
Florian Ludewig
  • 4,338
  • 11
  • 71
  • 137