当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript http.HttpHandler类代码示例

本文整理汇总了TypeScript中@angular/common/http.HttpHandler的典型用法代码示例。如果您正苦于以下问题:TypeScript HttpHandler类的具体用法?TypeScript HttpHandler怎么用?TypeScript HttpHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了HttpHandler类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: intercept

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (req.headers.get('No-Auth') === 'True') {
        return next.handle(req.clone());
    }

    if (localStorage.getItem('currentUser') != null) {
        const clonedreq = req.clone({
            headers: req.headers.set('Authorization', 'Bearer ' + localStorage.getItem('currentUser'))
        });
        return next.handle(clonedreq)
            .do(
            succ => { },
            err => {
                if (err.status === 401) {
                    this.router.navigateByUrl('/login');
                }
            }
            );
    } else {
        this.router.navigateByUrl('/login');
    }
}
开发者ID:bob1457,项目名称:REALClient,代码行数:22,代码来源:auth.interceptor.ts

示例2: intercept

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = JSON.parse(localStorage.getItem('currentUser'));
        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: {
                    Authorization: `Bearer ${currentUser.token}`
                }
            });
        }

        return next.handle(request);
    }
开发者ID:matKudDev,项目名称:CarRentSystem---frontend-,代码行数:13,代码来源:jwt.interceptor.ts

示例3: intercept

	intercept(req : HttpRequest <any>, next : HttpHandler) : Observable <HttpEvent<any>> {
			// Clone the request to add the new header
      const token = new TokenStorage();
      const tokenVal = token.getToken();
			const clonedRequest = req.clone({
			headers: req
				.headers
				.set('Authorization', tokenVal ? `Bearer ${ tokenVal}` : '')
		});

		// Pass the cloned request instead of the original request to the next handle
		return next.handle(clonedRequest);
	}
开发者ID:linnovate,项目名称:mean,代码行数:13,代码来源:header.interceptor.ts

示例4: intercept

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        let currentUser = this.authenticationService.currentUserValue;
        if (currentUser && currentUser.token) {
            request = request.clone({
                setHeaders: { 
                    Authorization: `Bearer ${currentUser.token}`
                }
            });
        }

        return next.handle(request);
    }
开发者ID:OmkarSadegaonkar,项目名称:BookRecommender,代码行数:13,代码来源:jwt.interceptor.ts

示例5: intercept

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // Add content-type for POST, PUT, PATCH automatically
        if (request.method === 'POST' || request.method === 'PUT' || request.method === 'PATCH') {
            request = request.clone({
                setHeaders: {
                    'Content-Type': 'application/json; charset=utf-8'
                }
            });
        }

        return next
            .handle(request)
            .catch(response => {
                let message: string;

                if (response instanceof HttpErrorResponse) {
                    switch (response.status) {
                        case 0: {
                            message = "No network connection";
                            break;
                        }
                        case 401: {
                            message = "Unauthenticated";
                            break;
                        }
                        case 403: {
                            message = "Unauthorized";
                            break;
                        }
                        case 404: {
                            message = "Resource Not Found";
                            break;
                        }
                        case 500: {
                            message = "Server error";
                            break;
                        }
                        default: {
                            message = response.status.toString();
                            break;
                        }
                    }
                }

                this.snackBar.open(message, null, {
                    duration: 2000
                });

                return Observable.throw(response);
            });
    }
开发者ID:pete33221100,项目名称:test-project-28032018,代码行数:51,代码来源:http-custom.interceptor.ts

示例6: intercept

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

 request = request.clone({
    headers: new HttpHeaders(
      {
        'Content-Type':  'application/json',
        'Authorization': 'desafio-4-devs' 
      }
    )  
  });
  
 return next.handle(request);

 }
开发者ID:gardheamsantos00,项目名称:4devs,代码行数:14,代码来源:interceptor.module.ts

示例7: intercept

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
     return next.handle(request).do(
         (event: HttpEvent<any>) => {},
         (err: any) => {
             if (err instanceof HttpErrorResponse) {
                 if (!(err.status === 401 && (err.message === '' || (err.url && err.url.indexOf('/api/account') === 0)))) {
                     if (this.eventManager !== undefined) {
                         this.eventManager.broadcast({ name: 'jHipsterRegistryApp.httpError', content: err });
                     }
                 }
             }
         }
     );
 }
开发者ID:gjik911,项目名称:git_01,代码行数:14,代码来源:errorhandler.interceptor.ts

示例8: intercept

 intercept(
     request: HttpRequest<any>,
     next: HttpHandler
 ): Observable<HttpEvent<any>> {
     const user: User = this.sessionStorage.retrieve('user');
     if (user && user.token) {
         request = request.clone({
             setHeaders: {
                 JWT: user.token,
             },
         });
     }
     return next.handle(request);
 }
开发者ID:RCata,项目名称:Work-Work,代码行数:14,代码来源:token-interceptor.ts

示例9: intercept

 intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
     return next.handle(request).pipe(
         tap(
             (event: HttpEvent<any>) => {},
             (err: any) => {
                 if (err instanceof HttpErrorResponse) {
                     if (err.status === 401) {
                         this.loginService.logout();
                     }
                 }
             }
         )
     );
 }
开发者ID:Doha2012,项目名称:tutorials,代码行数:14,代码来源:auth-expired.interceptor.ts

示例10: intercept

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        console.log(req.method);
        if (req.method == 'OPTIONS') {
            console.log("OPTIONS!");
        }
        console.log(req.body);
/*         if (req.url.startsWith("http://localhost:8080/")) {
            const newBody = this.toJsonString(req.body);
            const modified = req.clone({body: newBody});
            return next.handle(modified);
        } */
        console.log(req.url);
        return next.handle(req);
    }
开发者ID:Kerrex,项目名称:TrainingCenter_Frontend,代码行数:14,代码来源:api.interceptor.ts


注:本文中的@angular/common/http.HttpHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。