Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 385
Harshavardhan N Avatar asked May 30 '18 18:05

Harshavardhan N


People also ask

What are HTTP interceptors and how do you use them in angular?

HTTP Interceptors is a special type of angular service that we can implement. It's used to apply custom logic to the central point between the client-side and server-side outgoing/incoming HTTP request and response. Keep in mind that the interceptor wants only HTTP requests.

How do you use interceptors?

Whenever a request is made, the interceptors handle it in between. They can also identify the response by performing Rxjs operators. The interceptors do not initiate the handle method and handle the requests at their level. The interceptor is used to perform various functions and methods to perform specific tasks.


1 Answers

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')
like image 185
Vikas Avatar answered Oct 16 '22 17:10

Vikas