我正在通过get请求传递responseType:'blob'。效果很好。
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { RequestOptions, Response, ResponseContentType } from '@angular/http';
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/pdf');
return this.http.get(url, {
headers: headers,
responseType: 'blob'
}
如何通过发布请求传递相同的responseType?
我尝试过:
const headers = new Headers({
'Content-Type': 'application/pdf'
});
const options = new RequestOptions({headers: headers});
options.responseType = ResponseContentType.Blob;
return this.http.post(url, body, options)
但是它不起作用。我收到错误消息:类型为'{headers:Headers;的参数。 }”不可分配给“ RequestOptionsArgs”类型的参数。属性“标题”的类型不兼容。不能将类型“标题”分配给类型“标题”。
UPD 以下是我已经重新提出的请求:
const headers = new HttpHeaders({ 'Content-Type': 'application/pdf'});
return this.http.post(url, body, { headers, responseType:'blob' })
效果很好!非常感谢!
答案 0 :(得分:1)
答案 1 :(得分:0)
您不需要'@ angular / http'及其引用。不需要。仅使用“ @ angular / common / http”
options.responseType = 'blob'
答案 2 :(得分:0)
您需要使用的是json,
responseType:'blob' as 'json'
答案 3 :(得分:0)
对于更常见的情况,您还可以使用 http 拦截器。 例如:
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable()
export class SomeInterceptService implements HttpInterceptor {
// intercept request and parse custom response
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let customRequest;
const dataUrl = {
someUrl: '/api/someUrl',
};
for (const url in dataUrl ) {
if (dataUrl .hasOwnProperty(url)) {
customRequest = request.url.includes(dataUrl [url])
? request.clone({
...request,
responseType: 'blob',
})
: request;
}
}
return next.handle(customRequest).pipe(
map((response: HttpResponse<any>) => {
if (response instanceof HttpResponse) {
return response;
}
})
);
}
}