angular - 有什么方法可以改变 Angular 5 HttpInterceptor 中的响应吗?

标签 angular typescript rxjs angular-httpclient

对于我的应用程序,我创建了以下 HttpInterceptor。有没有办法从这里向请求订阅者返回响应的更改版本?

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

@Injectable()
export class RequestInterceptor implements HttpInterceptor {
    constructor(
        private router: Router
    ) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(request).do((event: HttpEvent<any>) => {
            if (event instanceof HttpResponse) {
                // any way to alter response that gets sent to the request subscriber?
            }
        }, (error: any) => {    
            if (error instanceof HttpErrorResponse) {
                if (error.status === 401 || error.status === 403) {
                    console.log('The authentication session has expired or the user is not authorised. Redirecting to login page.');
                    this.router.navigate(['/login']);
                }
            }
        });
    }
}

谢谢。

最佳答案

就像 Marcel Lamothe 在他的回答中指出的那样,您可以通过克隆事件和更改 body 属性来更改响应。

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

@Injectable()
export class RequestInterceptor implements HttpInterceptor {
    constructor(
        private router: Router
    ) {}

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(request).map((event: HttpEvent<any>) => {
            if (event instanceof HttpResponse) {
                // change the response body here
                return event.clone({
                    body: 'myCustomResponse'
                });
            }

            return event;
        }).do((event: HttpEvent<any>) => {}, (error: any) => {
            if (error instanceof HttpErrorResponse) {
                if (error.status === 401 || error.status === 403) {
                    console.log('The authentication session has expired or the user is not authorised. Redirecting to login page.');
                    this.router.navigate(['/login']);
                }
            }
        });
    }
}

关于angular - 有什么方法可以改变 Angular 5 HttpInterceptor 中的响应吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47400364/

相关文章:

javascript - 如何在 IONIC 3 中将多个输入字段作为 JSON 对象发布到服务器

angular - Angular 模板中的问号是什么?

node.js - 在同一 POST 请求上使用 multer (NodeJS) + 其他表单数据上传文件 (Angular)

typescript - 从 typescript 中引用自身的父对象推断窄类型

angular - 如何在调用我的 api 之前减慢/​​等待?

javascript - 创建后更改可观察对象的间隔/设置

javascript - 返回一系列 creationTimes 中的最近时间

javascript - 从数组中查找所有可能的匹配字符串和子字符串

javascript - 错误: You provided 'null' where a stream was expected

c# - 当返回类型为 iActionResult 时,component.ts 类给出错误