Issue
I've built a custom httpParams
function accepting a key value object as follows:
private httpParamsFactory(parameters: { [key: string]: string }): { params: HttpParams } {
const params: HttpParams = new HttpParams();
for (const [key, value] of Object.entries(parameters)) params.append(key, value)
return { params };
}
I'm using the httpParamsFactory
as following:
remove = (id: string): Observable<boolean> => this.http.delete<boolean>(`some/url`, this.httpParamsFactory({ id }))
for some reason the query strings aren't added to the URL, I tried debugging httpParamsFactory
and it turned out that the appending isn't working as console.log(params.keys())
returned an empty array.
Solution
Try this..
Method 1
private httpParamsFactory(parameters: { [key: string]: string }): { params: HttpParams } {
let params: HttpParams = new HttpParams();
for (const [key, value] of Object.entries(parameters)) {
params = params.append(key, value)
}
return { params };
}
Method 2
private httpParamsFactory(parameters: { [key: string]: string }): { params: HttpParams } {
const params = new HttpParams({ fromObject: parameters});
return { params };
}
Reference
Answered By - hbamithkumara
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.