使用HttpClient和HttpClient的Angular Http完整响应

时间:2019-02-19 11:53:29

标签: angular angular-httpclient angular-http-interceptors

我在Angular 4中使用了许多http请求。

现在,我将以HttpClient的完整响应升级到Angular 6。

我将尝试在{observe: 'response'}的{​​{1}}参数中

我的代码如下

HttpInterceptor

但它不起作用

确保它运行良好

import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth/auth.service';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
  constructor(public auth: AuthService) {}
  intercept(request: HttpRequest<any>, next: HttpHandler): 
  Observable<HttpEvent<any>> {

    request = request.clone({
      setParams: { observe: 'response' },
      setHeaders: {
        Authorization: `Bearer ${this.auth.getToken()}`
      }
    });
    return next.handle(request);
  }
}

我已经发现我的问题非常相似(链接:Is there a way to inject observe: 'response' from HttpInterceptor in Angular 6?

可悲的是,他似乎只找到了答案。我问你是否真的不可能。

1 个答案:

答案 0 :(得分:0)

取决于您要执行的操作;我以前也没有观察到响应,但是我一直在标题后面并成功使用了它:

打字稿

const httpOptions = {
        observe:'response',
    };
this.httpClient.post(url, data, httpOptions) // this errors on typeerror (so comment out the 'observe')
.subscribe(resp => {
  console.log('resp',(resp as any).headers.get('Location')) // we still actually have access to it
})