简体   繁体   中英

angular5 and http GET query parameters - looks like dedicated object (HttpParams or UrlSearchParams) do not work

I have a strange problem I cannot understand. Here is the code calling REST endpoint:

this.http.get<AllApplicationType[]>(environment.SUDS_API_SERVICE_URL + environment.SUDS_ALL_APPLICATIONS_URL, this.setQueryParams(page, size)).toPromise() ...

where setQueryParams function look like this:

setQueryParams(page?: number, size?: number): {} {
const startFrom = page * size + 1;
const params = new HttpParams();
params.set('startFrom', startFrom.toString());
params.set('maxRecords', size.toString());
return params;

}

When the request comes to my backend query params are null, somehow they are not being passed over but why ? Is this not the right method or what ?

It just occurred that using HttpParams or URLSearchParams objects do not work at all. All tries with these 2 failed in my case. The only way is to build parameters explicitly like this:

setRequestParams(page?: number, size?: number): {} {
const startFrom = page * size + 1;
const params = {'startFrom': startFrom.toString(), 'maxRecords': size.toString()};
const options = {params: params};
return options;

}

Well I beleive this is a bug in Angular HttpClient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM