2

Server returns data in this format: {"query": 'queryName', 'result': []}.

I need to get only result part, for that I did this:

export class RequestInterception implements HttpInterceptor {

  public constructor(private route: Router) {

  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next.handle(request).do((event: HttpEvent<any>) => {
     if (event instanceof HttpResponse) {
            return event.body['result'];
     }
    }, (err: any) => {
        if (err instanceof HttpErrorResponse) {
             if (err.status === 401) {
               this.route.navigate(['/login']);
             }

          return throwError('backend comm error');

        }
    })

};

Inside do operator I tried this:

return event.body['result'];

But it still returns me whole object instead.

AppModule is:

 providers: [
    {
      provide: HTTP_INTERCEPTORS,
      useClass: RequestInterception,
      multi: true
    },
  ],
3
  • 1
    Are you using an interceptor? Commented Oct 30, 2018 at 15:13
  • Sure, I use it: intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {} Commented Oct 30, 2018 at 15:15
  • 1
    Can you try logging event.body['result'] to the console? Commented Oct 30, 2018 at 15:18

2 Answers 2

1

If you want to transform the response in the interceptor, then you can do it with a map operator. You can also use the catchError operator and then inside that, use throwError in case the status code is 401.

Something like this:

import { Injectable } from '@angular/core';
import { 
  HttpInterceptor, HttpRequest, HttpResponse, 
  HttpHandler, HttpEvent, HttpErrorResponse 
} from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Router } from '@angular/router';

@Injectable()
export class InterceptorService implements HttpInterceptor {

  constructor(private route: Router) { }

  intercept(
    req: HttpRequest<any>, 
    next: HttpHandler
  ) {
    return next.handle(modified)
      .pipe(
        map((event: HttpResponse<any>) => {
          event['body'] = event.body['result'];
          return event;
        }),
        catchError((error: HttpErrorResponse) => {
          if (error.status === 401) {
            this.route.navigate(['/login']);
          }
          return throwError('backend comm error');
        })
      );
  }

}

Here's a Sample StackBlitz for your ref.

Sign up to request clarification or add additional context in comments.

3 Comments

I get an error on: event['body'] = { foo: 'bar' };
Cause it is constant
My bad. I just pasted the same code from the stackblitz. It should be event['body'] = event.body['result']; I've updated the answer.
1

An HttpReponse behaves like a JSON. For example, in order to get the body of your response you can do the following:

response_body = response["body"]

Of course, you have to subscribe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.