1

In an Angular application, I use an interceptor to inject the token (when needed) into the request headers. I also need it to modify the Content-Type header to the appropriate one. By default, it must be application/json, but there is a call that needs to upload a ZIP file, so I want to let the Content-Type header as is (for these cases, it's something like: Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvamnUWepE840OEuq). I use this code:

    intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        let usr = this.userService.getActualUser();
        let confirmationToken = '';
        let credentials = false;
        let contentType = 'application/json';
        
        // Check if the incoming request has an specific content type header
        if (req.headers.has('Content-Type')) {
          contentType = req.headers.get('Content-Type');
        }
    
        // ETC
    }

The problem is that req.headers.has('Content-Type') always returns false, so I'm setting the wrong type and the call fails. What am I doing wrong?

Thanks in advance!

3 Answers 3

1

I believe the content of your form is an instance of the FormData object? If so,

if(req.body instanceof FormData)

This check might help you or give you some hints if you are unable to access the headers. Or maybe you could even dive deeper and check into the req.body object to do a check that is more specific.

Sign up to request clarification or add additional context in comments.

Comments

1

During your request, you would expect to see the content-type but you won't unless you directly specify this.

If you have this on your request, you will see the content type.

return this.http.get(this.ENDPOINT, {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
})

This, however, could be an overkill.

Another possible solution is to check the instance type of the body and then set the headers accordingly

if (req.body instanceof FormData) {
    contentType = 'multipart/form-data';
}

3 Comments

Hi! I tried this solution but the call ends with an error stating 422 Unprocessable Entity. I think it's because it expects the boundary value... Do you know how could I get this value? Thanks!
You pointed me to the right direction, the logic I needed was actually quite simple but I needed more coffee today to see it clear :)
if my answer fulfills your original question, please vote up
1

Following your advices, I came up with a very simple solution, that is leave the Content-Type header as is if the request body is form data, and set is as JSON for the rest. Something like:

    if(!(req.body instanceof FormData)) {
      contentType = 'application/json';
    }

Cheers!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.