Search code examples
angularangular-http-interceptors

How to remove Content Type set in HTTP Interceptors for uploading a file in angular4


In my application we set content-type = application/json in interceptor. But to upload a file content-type should be multipart/form-data ,i.e it will take contant-type = multipart/form-data when we try to upload form data. my question is how can I remove the content type set in interceptor while doing a post request to upload a file.

thanks, Harshavardhan


Solution

  • To Remove Existing header

     if (!req.headers.has('Content-Type')) {
     req = req.clone({ headers: req.headers.delete('Content-Type','application/json') });
    

    Adding new Headers

    req = req.clone({ headers: req.headers.set('Content-Type', 'multipart/form-data')})
    

    To check the current value of the header.

    req.headers.get('Accept')