5

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.
   }
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

2 Answers2

5

If 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.com/angular/angular/blob/master/packages/common/http/src/params.ts#L177-L186

Ng-run Example

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • 1
    Oh my god I was so wrong. [I send POST so I should've investigate `req.body` and not `req.params`](https://i.imgur.com/eTvhF9I.jpg) . thank you yurzui. Like always. ( _btw it worked without looping , straight json.stringify on req.body.params_) – Royi Namir Feb 22 '18 at 16:27
  • Do you know the reason for why `req.body.params` is not lazy , while `req.params` is ? It doesn't make any sense ( to me) – Royi Namir Feb 22 '18 at 18:39
  • `Bodies are not enforced to be immutable, as they can include a reference to any user-defined data type. However, interceptors should take care to preserve idempotence by treating them as such.` https://github.com/angular/angular/blob/master/packages/common/http/src/request.ts#L80-L86 – yurzui Feb 22 '18 at 18:44
  • Oh , it's jsut copy paste char from [here](https://www.google.com/search?q=thank+you+unicode&rlz=1C1NHXL_iwIL704IL704&oq=thank+you+unicode&aqs=chrome..69i57j0l5.2934j0j4&sourceid=chrome&ie=UTF-8) – Royi Namir Feb 22 '18 at 18:46
2

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)
Melroy van den Berg
  • 2,697
  • 28
  • 31