#How to use HttpParams to create URL?

8 messages · Page 1 of 1 (latest)

opaque surge
#

I have definition of interface like this

export interface GetRandomName_Get { Len: number; }

If I write

GetRandomName(item: GetRandomName_Get): Observable<any> { var url = "/Aes/GetRandomName"; let queryParams = new HttpParams({ fromObject: item }); return this.http.get(url, { params: queryParams }).pipe(); }

I expect this is all I need for serialization object to UrlParameters, but no, unexpectedly fromObject method is not working

Type 'GetRandomName_Get' is not assignable to type '{ [param: string]: string | number | boolean | readonly (string | number | boolean)[]; }' Index signature for type 'string' is missing in type GetRandomName_Get' .ts(2322)

Pass item as Object (with curly bracket) don't working too.

radiant ruin
#

parameters are part of the URL. They can only be strings, numbers, booleans, of arrays of those. Not objects. That's what the error is telling you.

#

Also, don't use var, don't use any, respect the naming conventions, type your http request method calls...

opaque surge
#

@radiant ruin , thank u, but what correct syntax in my case?

radiant ruin
#

It's not a matter of syntax. It's a matter of doing something that makes sense. You shouldn't try to send a whole object as a query parameter, because that's not how the web works. Parameters are simple values (strings).

opaque surge
#

ok, but I need serializator to create URL parameters from object, how is possible create ordinary URL parm as ?a=12345&b=abcdfgh from object? I was thinking UrlParametrs-fromobject exactly what I need.

radiant ruin