我希望在来自Angular 4应用程序的API项目的传出POST和PUT JSON请求上使用gzip或deflate压缩。目前,我正在使用HttpClient发送这些请求。我已经尝试使用pako或zlib生成压缩内容,但服务器返回的响应表明压缩算法的实现有问题。我的POST TypeScript代码如下:
我尝试了各种排列组合的方法来压缩和解压内容,但是似乎都不起作用。我还在 Fiddler 中检查了发出的请求并验证了 Fiddler 无法解释请求的 JSON 数据。
我还验证了内容是以 Content-Type: application/json; charset=UTF-8 和 Content-Encoding: deflate 的方式发送,并带有适当的 Accept-Encoding 值。
目前,我确定要么是我做错了什么还没有弄清楚,要么就是我试图做超出 HttpClient 允许范围的事情。
public post(url: string, content: any): Observable < any > {
const fullUrl: string = `${HttpService.baseUrl}/${url}`;
Logger.debug(`Beginning HttpPost invoke to ${fullUrl}`, content);
// Optionally, deflate the input
const toSend: any = HttpService.compressInputIfNeeded(content);
return Observable.create((obs: Observer < any > ) => {
this.client.post(fullUrl, toSend, HttpService.getClientOptions()).subscribe(
(r: any) => {
Logger.debug(`HttpPost operation to ${fullUrl} completed`, r);
// Send the response along to the invoker
obs.next(r);
obs.complete();
},
(err: any) => {
Logger.error(`Error on HttpPost invoke to ${fullUrl}`, err);
// Pass the error along to the client observer
obs.error(err);
}
);
});
}
private static getClientOptions(): {
headers: HttpHeaders
} {
return {
headers: HttpService.getContentHeaders()
};
}
private static getContentHeaders(): HttpHeaders {
let headers: HttpHeaders = new HttpHeaders({
'Content-Type': 'application/json; charset=utf-8'
});
// Headers are immutable, so any set operation needs to set our reference
if (HttpService.deflate) {
headers = headers.set('Content-Encoding', 'deflate');
}
if (HttpService.gzip) {
headers = headers.set('Content-Encoding', 'gzip');
}
return headers;
}
private static compressInputIfNeeded(content: any): string {
const json: string = JSON.stringify(content);
Logger.debug('Pako Content', pako);
if (HttpService.deflate) {
const deflated: string = pako.deflate(json);
Logger.debug(`Deflated content`, deflated);
return deflated;
}
if (HttpService.gzip) {
const zipped: string = pako.gzip(json);
Logger.debug(`Zipped content`, zipped);
return zipped;
}
return json;
}
我还验证了内容是以 Content-Type: application/json; charset=UTF-8 和 Content-Encoding: deflate 的方式发送,并带有适当的 Accept-Encoding 值。
目前,我确定要么是我做错了什么还没有弄清楚,要么就是我试图做超出 HttpClient 允许范围的事情。