Alex Cooper
Alex Cooper

Reputation: 597

ContentType for HttpHeaders when uploading File in FormData

I have an Angular Single Page Application which calls a .NET Core API.

The default set up creates HttpHeaders to pass a jwt token to the API and sets the content-type to application/json; charset=utf-8

To extend this application, I've been following "Angular Image Upload Made Easy" (https://www.youtube.com/watch?v=YkvqLNcJz3Y), but with my default headers, the image in FormData in the API was always null.

So I changed the content-type to multipart/form-data, but got the same result.

After much experimentation, I discovered that not setting a content-type at all works. I can still pass my jwt to authenticate the user and the image now uploads without any problems.

So, is it safe to leave out content-type in request options, or would it be better to insert the "correct" type and what would that be?

(In the example in the video, request options are added to monitor progress of the upload, but no headers are included - reportProgress:true, observe:'events')

Upvotes: 0

Views: 801

Answers (1)

Leccho
Leccho

Reputation: 666

This should not be a problem but you still have to make file type verification in your .NET core API.

In requests, (such as POST or PUT), the client tells the server what type of data is actually sent.

Source

Upvotes: 1

Related Questions