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


TypeScript http.HttpRequest类代码示例

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


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

示例1: intercept

  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    if (!this.auth) {
      this.auth = this.injector.get(AuthService);
    }

    if (this.auth.isAuthenticated()) {
      request = request.clone({
        setHeaders: {
          'X-Auth-Token': this.auth.getToken()
        }
      });
    }

    // Add CSRF token for the Play CSRF filter
    const token = this.cookieService.get('PLAY_CSRF_TOKEN');
    if (token) {
      // Play looks for a token with the name Csrf-Token
      // https://www.playframework.com/documentation/2.4.x/ScalaCsrf
      request = request.clone({
        setHeaders: {
          'Csrf-Token': token
        }
      });
    }

    return next.handle(request);
  }
开发者ID:epot,项目名称:Gifter,代码行数:30,代码来源:token-interceptor.ts

示例2: intercept

 intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
   const token: string = this._auth.token;
   if (token) {
     req = req.clone({
       setHeaders: {
         Authorization: `Bearer ${token}`,
         ContentType: 'application/json; charset=utf-8'
       }
     });
   } else {
     req = req.clone({
       setHeaders: {
         ContentType: 'application/json; charset=utf-8'
       }
     });
   }
   return next.handle(req).pipe(
     tap(
       (event: HttpEvent<any>) => {
         if (event instanceof HttpResponse) {
           // do stuff with response if you want
         }
       },
       (err: any) => {
         if (err instanceof HttpErrorResponse) {
           if (err.status === 401) {
             this._auth.collectFailedRequest(req);
             // console.log(err);
           }
         }
       }
     )
   );
 }
开发者ID:digitaldeacon,项目名称:memberhive,代码行数:34,代码来源:auth.interceptor.ts

示例3: setReq

 setReq(req: HttpRequest<any>, options: DelonAuthConfig): HttpRequest<any> {
   const token = options.token_send_template.replace(
     /\$\{([\w]+)\}/g,
     (_: string, g) => this.model[g],
   );
   switch (options.token_send_place) {
     case 'header':
       const obj = {};
       obj[options.token_send_key] = token;
       req = req.clone({
         setHeaders: obj,
       });
       break;
     case 'body':
       const body = req.body || {};
       body[options.token_send_key] = token;
       req = req.clone({
         body: body,
       });
       break;
     case 'url':
       req = req.clone({
         params: req.params.append(options.token_send_key, token),
       });
       break;
   }
   return req;
 }
开发者ID:wexz,项目名称:delon,代码行数:28,代码来源:simple.interceptor.ts

示例4: intercept

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const loginService = this.injector.get(LoginService);
    const authHeader = loginService.getAuthorizationHeader();
    const isAuthHeaderNeeded = !(req.url.includes('/account') || req.url.includes('/authenticate'));
    let authReq = null;
    if (isAuthHeaderNeeded) {
      if (loginService.isAuthenticated()) {
        authReq = req.clone({headers: req.headers.set('Authorization', authHeader), responseType: 'text'})
      } else {
        console.log('JWT expired');
        loginService.logout();
        this.router.navigate(['']);
      }
    } else {
      authReq = req.clone({responseType: 'text'});
    }

    return next.handle(authReq)
        .map((event: HttpEvent<any>) => {
          if (event instanceof HttpResponse) {
            return event.clone({ body: JSON.parse(event.body), });
          }
        })
        .catch((error: HttpErrorResponse) => {
          const parsedError = Object.assign({}, error, {error: JSON.parse(error.error)});
          return Observable.throw(new HttpErrorResponse(parsedError));
      });

  }
开发者ID:edouardo,项目名称:schedule-sys,代码行数:29,代码来源:auth.intercepter.ts

示例5: intercept

  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    if (!req.headers.has("Content-Type")) {
      req = req.clone({
        headers: req.headers.set("Content-Type", "application/json")
      });
    }

    req = req.clone({ headers: req.headers.set("Accept", "application/json") });

    if (this.auth.getToken() !== null) {
      req = req.clone({
        headers: req.headers.set("x-access-token", this.auth.getToken())
      });
    }

    return next.handle(req).do(evt => {
      if (evt instanceof HttpResponse) {
        console.log("---> status:", evt.status);
        console.log("---> filter:", req.params.get("filter"));
      }
    });
  }
开发者ID:binishjohn,项目名称:notus,代码行数:25,代码来源:interceptor.service.ts

示例6: intercept

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (!req.headers.has('Content-Type')) {
      req = req.clone({ headers: req.headers.set('Content-Type', 'application/json') });
    }

    req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
    console.log(JSON.stringify(req.headers));
    return next.handle(req);
  }
开发者ID:geogikei,项目名称:INTEXO_PE_WEB,代码行数:9,代码来源:data.service.ts

示例7: intercept

 intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
     // Clone the request to add the new header.
     const token = this.token.getToken();
     const tenantId = this.token.getTenantId();
     let authReq = req.clone();
     if (token) {
         authReq = req.clone({headers: req.headers.set('Authorization', 'Bearer ' + token)});
     }
     if (tenantId) {
         authReq = authReq.clone({headers: authReq.headers.set('X-TENANT-ID', tenantId)});
     }
     // Pass on the cloned request instead of the original request.
     return next.handle(authReq);
 }
开发者ID:yangjinguang,项目名称:liyu,代码行数:14,代码来源:http-provider.service.ts

示例8: intercept

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const token: string = localStorage.getItem('token');
    if (token) {
      // "Authorization : Bearer" format are most likely implementing OAuth 2.0 bearer tokens.
      request = request.clone({headers: request.headers.set('Authorization', 'Bearer ' + token)});
    }
    if (!request.headers.has('Content-Type')) {
      request = request.clone({headers: request.headers.set('Content-Type', 'application/json')});
    }
    request = request.clone({headers: request.headers.set('Accept', 'application/json')});
    console.log('%cHttpConfigInterceptor HttpRequest', 'color:yellow', request);

    // this deals with the response
    return next.handle(request).pipe(map(this.onSuccess), catchError(this.onError));
  }
开发者ID:screenm0nkey,项目名称:angular2-examples-webpack,代码行数:15,代码来源:httpconfig.interceptor.ts

示例9: intercept

  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    // Check if there is a token saved in localStorage
    // const token: string = localStorage.getItem(AppSettings.global.tokenName);

    console.log('AuthInterceptor Invoked');
    const token =
      // tslint:disable-next-line:max-line-length
      'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1YmY1OGQ3ZDhiNDgwNTFlZThhZWQzMDAiLCJlbWFpbCI6ImNvc3RhX0l2bzNAZ21haWwuY29tIiwiaWF0IjoxNTQ0NjM1ODM2LCJleHAiOjE1NTMyNzU4MzZ9._cYkDjYwwUzOvzqp0HS8R86IgyyMmRCAgGGH0JEW3Ho';

    if (token === null) {
      // Pass on  the original request.
      return next.handle(req);
    } else {
      const authHeader = 'Bearer ' + token;

      // Cloning Each Incoming Request as Incoming request is Immutable
      const authReq = req.clone({
        headers: req.headers
          .set('Authorization', authHeader)
          .set('Content-Type', 'application/json')
      });

      // Pass on the cloned request instead of the original request.
      return next.handle(authReq);
    }
  }
开发者ID:costaivo,项目名称:AngularMeteor-AdManager,代码行数:29,代码来源:auth.interceptor.ts

示例10: switchMap

 switchMap((token) => {
   return next.handle(req.clone({
     setHeaders: {
       Authorization: token
     }
   }));
 })
开发者ID:farleydiaz,项目名称:farley-test,代码行数:7,代码来源:AuthInterceptor.ts


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