Remove null params in HttpParams Remove null params in HttpParams angular angular

Remove null params in HttpParams


You should filter them client side using JavaScript's delete (MDN) keyword if they have a value of undefined (which is different than the key not being in the object at all):

if (queryObject.secsymb === undefined) {  delete queryObject.secsymb;}

and then use a default method parameter in your controller method:

GetExposureRunsData(int id, string fund, SecType sectype, string secsymb = null)

If you ensure that the query string parameter is not included then your parameter will default to null


That's what I usually do. Works like a charm.

    getDocumentsReport(filter: object) {        let params = new HttpParams();        Object.keys(filter).forEach(            key => filter[key] && (params = params.append(key, filter[key]))        );            return this.http.get<object>(            environment.apiServer + '/api/reports/documents',            {                params            }        );    }


You can try this:

this.httpClient.get('../api/path', {   params: Object.entries(queryParameterObject).reduce((queryParams, [key, value]) => queryParams.set(key, value), new HttpParams());});

It worked for me.