I'm using Angular 5 with HttpInterceptors.
I already know I can get each value I want from HttpParams
via several methods.
Also - If I want to see all values , I can use the .toString()
method
params = new HttpParams()
.set('page', '2')
.set('sort', 'name');
console.log(params.toString()); //Returns page=2&sort=name
But in my case I send json objects as parameters :
{
a:1 , b:[1,2,3] , c:[{...}]
}
I'm using interceptors to log the request parameters , but when I JSON.stringify(req.Params)
, I get :
Params={
"updates": null,
"cloneFrom": null,
"encoder": {},
"map": {}
}
Which doesn't expose the values.
I don't want to see the parameters as a regular form post parameters -( it will be very unclear), but as an object as I've sent it.
Question:
How can I extract the parameters from the request object in the interceptor , but as json format :
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
{
JSON.stringify( req.params) // <--- ?? doesn't yield the params.
}
I'm using Angular 5 with HttpInterceptors.
I already know I can get each value I want from HttpParams
via several methods.
Also - If I want to see all values , I can use the .toString()
method
params = new HttpParams()
.set('page', '2')
.set('sort', 'name');
console.log(params.toString()); //Returns page=2&sort=name
But in my case I send json objects as parameters :
{
a:1 , b:[1,2,3] , c:[{...}]
}
I'm using interceptors to log the request parameters , but when I JSON.stringify(req.Params)
, I get :
Params={
"updates": null,
"cloneFrom": null,
"encoder": {},
"map": {}
}
Which doesn't expose the values.
I don't want to see the parameters as a regular form post parameters -( it will be very unclear), but as an object as I've sent it.
Question:
How can I extract the parameters from the request object in the interceptor , but as json format :
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>
{
JSON.stringify( req.params) // <--- ?? doesn't yield the params.
}
Share
Improve this question
asked Feb 22, 2018 at 15:56
Royi NamirRoyi Namir
149k144 gold badges492 silver badges829 bronze badges
12
-
can you post how you add those objects to the
HttpParams
object? – messerbill Commented Feb 22, 2018 at 16:00 - @messerbill SURE i.imgur./Sgn3Ak8.jpg – Royi Namir Commented Feb 22, 2018 at 16:03
- 2 That's because they are lazy. See also stackoverflow./questions/46418746/… – yurzui Commented Feb 22, 2018 at 16:04
- @yurzui Hi. If so , how can I eagerly extract them as Json.stringify ? – Royi Namir Commented Feb 22, 2018 at 16:05
- @RoyiNamir This helps 0. – messerbill Commented Feb 22, 2018 at 16:07
2 Answers
Reset to default 8If you don't like method toString()
that returns an encoded string, where key-value pairs (separated by =
) are separated by &
s you can write your own method that will transform data stored in Map in some data you want to use.
For example:
const params = new HttpParams()
.set('page', '2')
.set('sort', 'name');
const paramsArray = params.keys().map(x => ({ [x]: params.get(x) }));
console.log(JSON.stringify(paramsArray));
It's similar to the approach that is used in toString
method
https://github./angular/angular/blob/master/packages/mon/http/src/params.ts#L177-L186
Ng-run Example
What you actually requested was a JSON Object. So this is how you get such an object:
const params = new HttpParams()
.set('page', '2')
.set('sort', 'name');
const paramsObject = params.keys().reduce((object, key) => {
object[key] = params.get(key)
return object
}, {})
console.log(paramsObject)
// And json if you really want
const json = JSON.stringify(paramsObject)