在一个请求中禁用aaplication / json

时间:2019-04-20 08:11:16

标签: javascript angular typescript

我编写此代码以发送请求以上传文件:

 const uploadReq = new HttpRequest('POST', "https://localhost:44372/api/v1/Upload/UploadNewsPic"
  , formData, { reportProgress: true });
  this.http.request(uploadReq).subscribe(event => {
    if (event.type === HttpEventType.UploadProgress)
      this.progress = Math.round(100 * event.loaded / event.total);
    else if (event.type === HttpEventType.Response)
      this.messsage = event.body.toString();
  })

并且我使用拦截器将自动application/json添加到标头中,但是我无需为此请求添加此标头。

我该怎么做??????????

1 个答案:

答案 0 :(得分:1)

如果您有权使用拦截器,则可以检查url并避免为此特定请求添加此标头:

@Injectable()
export class AddHttpHeaderService implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const url = request.url;

    if (url === 'https://localhost:44372/api/v1/Upload/UploadNewsPic') {
      return next.handle(request);
    }

    // other part of interceptor related to adding application/json to header
    ...