问题描述
我在我的角度应用程序中有一个特定的请求,在该应用程序中,我将带有图像细节对象的图像上传到服务器。我在流程中具有模块的所有api请求的拦截器,以为服务器添加一些公共信息,但是对于此特定请求,我不需要将 Content-Type 标头设置为默认值到application / json。
默认情况下,Angular拦截器默认添加了application / json,用于文件上传请求
"Content-Type": null
"Accept": multipart/form-data
当我将其放在服务方法中时,上面的方法在没有拦截器的情况下有效,那么如何在拦截器中修改Content-Type标头并将其设置为null?
主要问题如下:
request.clone({body: examplebodyObject,setHeaders: { 'Content-Type': null }});
在拦截器中不接受null 或 undefined 作为标头值,我也尝试过从请求中删除Content-Type标头,但仍然无法正常工作
我现在有以下代码:
request = request.clone({ body: this.someFunction(request,otherParam),setHeaders: {
'Accept': 'multipart/form-data'
} });
request.headers.delete('Content-Type');
解决方法
您应该根据克隆的请求修改标头,并将克隆的请求传递给下一个处理程序。
const clonedReq = request.clone()
clonedReq.headers.delete('Content-Type');
return next.handle(clonedReq);
,
尝试解决此问题How to add multiple headers in Angular 5 HttpInterceptor
像这样跳过内容类型:
const newReq = req.clone({
body: examplebodyObject,headers: new HttpHeaders({
Accept: 'multipart/form-data'
})
});