First of all, a content type is an HTTP header used to indicate what the content type is being sent or received. It generally indicated the type of media type sent via the POST and PUT, where the client tells the server about the type of data sent.
The browser does a MIME sniffing to find out what the content is present. MIME, otherwise known as "multipurpose internet mail extension," is a specification used in the mail to determine what type of content or attachment expect text has been sent over the mail.
In the case of receiving a JSON request, it is vital to mention the type of content the browser is about to receive.
So, we set the MIME type by mentioning its content type in this case.
Two types with which we can do this:
- MIME type: application/JSON
MIME type: application/json
- This header is sent when we have no clue about our content.
- When information is just to be extracted in JSON format, maybe through a file or link, then this header is used.
- It can be terminated from the front-end framework after the content inside the link has been fetched in JSON format.
response.setHeader("Accept", "application/json"); response.setHeader("Content-type", "application/json");
- When the use of data is predefined, this type is used.
- It is always recommended to use the application/JSON type because JSON data is not seen as a js code.
- As discussed above, the server always needs the type of data it's about to receive, using the POST method.
- Without encoding the header with these types, the POST won't work.