无法在Nativescript Http中获取set-cookie标头

时间:2019-06-04 04:39:32

标签: angular nativescript

我尝试从HttpInterceptor类获取http标头。 当我尝试记录标题时,不存在set-cookie。 但是,当我尝试卷曲URL时,会出现set-cookie。

这是我尝试访问标头的HttpInterceptor类:

import { Injectable } from "@angular/core";
import { SessionService } from "../session/session.service";
import { HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from "@angular/common/http";
import { Observable } from "rxjs";
import { tap } from "rxjs/operators";

@Injectable()
export class HttpRequestInterceptor {
  constructor(private sessionService: SessionService) {

  }

  private isCachable(req: HttpRequest<any>): boolean {
    if (req.method != 'GET' && req.method != 'OPTIONS') {
      return false; 
    }

    return true;
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const successHandler = (response) => {
      // Intercept ecopayz successful form response.
      if (!this.isCachable(request) && response.headers && response.headers.get('x-redirect-url')) {
        window.location = response.headers.get('x-redirect-url');
      }

      var headers = response.headers;
      if (headers) {
        headers.headers.forEach((value, key) => {
          console.log(key, value);
        });
      }
    };

    const errorHandler = (error): any => {
      if (error instanceof HttpErrorResponse ) {
        if (error.status === 401) {
          this.sessionService.clearSession();
        }
      }
    };

    request = request.clone({
      withCredentials: true
    });

    return next.handle(request).pipe(
      tap(successHandler, errorHandler),
    );
  }
}

PS:set-cookie标头具有HttpOnly标志。

为什么会这样?

谢谢!

1 个答案:

答案 0 :(得分:0)

get(url: string, options: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        observe: 'response';
        params?: HttpParams | {
            [param: string]: string | string[];
        };
        reportProgress?: boolean;
        responseType: 'blob';
        withCredentials?: boolean;
    }): Observable<HttpResponse<Blob>>;

这是http类型定义文件。

首先,您需要在选项中添加“观察:响应”。

这是文件下载功能,但只需查看代码即可。

正是这个选项->观察:“响应”

 public download(url: string) {
    return this.httpClient
      .get(url, {
        headers: new HttpHeaders({
          'Accept': '*/*;',
          'Content-Type': 'application/octet-binary'
        }),
        responseType: 'blob',
        observe: 'response'
      });
  }