Reputation: 692
I want to get the query params from an get request. Interceptor code is this way,
export class FixtureInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req);
}
}
i tried to get the params using get method like this ,
req.params.get('category');
but it returns me null always. My api call is this way ,
getValue() {
return this.http.get('http://139.49.10.175:3000/laundryItems?category=2&type=4');
}
i need value of category and type from above API call
Upvotes: 3
Views: 12511
Reputation: 3681
Well... here we are again... HttpParams documentation states object is immutable. Therefore, in order to build the collection we must do something like:
let params = new HttpParams().set("categoryName", "Cars");
if (search.length >= 2) {
params = params.append("search", search);
}
Adding to http.get
must take this form:
this.http.get<Product[]>(`${this.baseUrl}products`, { params }).subscribe(result => {
this.products = result;
}, error => {
console.error(error)
});
IMPORTANT: The wrapping of
params
in brace-brackets is required - especially if you want to retrieve the params, like by some interceptor - for example:
let search = request.params.get("search");
Also, refer to the section about using HttpParams in this article.
Upvotes: 0
Reputation: 2416
Apparently params directly putted in url doesn't appear in req.params
, this one should work:
getValue() {
return this.http.get('http://139.49.10.175:3000/laundryItems', {params: {category: '2', type: '4'}});
}
Upvotes: 3