HTTP 拦截器未完成(即永远不会调用 finalize)



在我的 angular 8 项目中,我实现了最简单的HttpInterceptor,它只是传递请求,而不做任何操作

在我的 angular 8 项目中,我实现了一个简单的HttpInterceptor,它只是克隆原始请求并添加一个参数:

@Injectable()
export class RequestHeadersInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// original code return next.handle(request) // pass-by request as-is
return next.handle(request.clone({
params: request.params.set('language', 'en') }
));
}
}

在我的服务中,我有一个getFoos()方法,该方法进行 HTTP 调用,该调用将被RequestHeadersInterceptor拦截:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { finalize } from 'rxjs/operators';
import { Foo } from '.';
@Injectable({
providedIn: 'root'
})
export class FooService {
constructor(private http: HttpClient) { }
getFoos() {
return this.http.get<Foo[]>('/foos')
.pipe(
finalize(() => console.log('observable completed!'))
);
}
}

在我的组件中,我终于订阅了getFoos()

fooService.getFoos().subscribe(console.log);

预期输出

[{ foo: 1 }, { foo: 2 }]
observable completed!

实际输出

[{ foo: 1 }, { foo: 2 }]

如您所见,finalize永远不会触发。为什么?

笔记

  • 如果删除拦截器,则会触发finalize,这是两种情况的预期行为
  • 我如何为模块提供拦截器:
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { RequestHeadersInterceptor } from './shared/http-requests';
@NgModule({
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: RequestHeadersInterceptor, multi: true },
]
);
  • 我更新了拦截器代码,因为我错误地指出,即使按原样传递请求,问题仍然存在。相反,它需要克隆和更改。

  • 我添加了一个演示,基于 @PierreDuc 的演示(主要道具!但是,我无法在演示中重现该问题。这可能与某些请求或响应标头有关。

实时系统 API 上的响应标头

Cache-Control: no-store, no-cache, must-revalidate, max-age=0 Cache-Control: post-check=0, pre-check=0
Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0
Connection: keep-alive
Content-Language: en-US
Content-Length: 42
Content-Type: application/json;charset=utf-8
Date: Tue, 21 Jan 2020 15:44:33 GMT
Pragma: no-cache
Pragma: no-cache
Server: nginx/1.16.1
X-Content-Type-Options: nosniff
X-Powered-By: Servlet/3.1

实时系统 API 上的请求标头

Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Authorization: Basic xyzABC123
Cache-Control: no-cache
Connection: keep-alive
Content-Type: application/json
Cookie: check=true; anotherCookie=1; bla=2;
Host: some.page.com:11001
Pragma: no-cache
Referer: https://some.page.com
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36

"issue"是Connection: Keep-Alive标头。这会保持打开的连接

连接常规标头控制网络连接在当前事务完成后是否保持打开状态。如果发送的值保持活动状态,则连接是持久的且不会关闭,从而允许对同一服务器的后续请求完成。

这将导致无法完成的可观察,直到连接终止。

因此,这不是您这边的错误或问题。我想在你的HttpInterceptor你添加了这个标题,所以这就是它只有在你添加拦截器后才引起的原因

最新更新