Skip to main content

JSON Content Type

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:

  1. MIME type: application/JSON
  2. MIME type: application/javascript

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.

Syntax

Java:

response.setHeader("Accept", "application/json");
response.setHeader("Content-type", "application/json");

MIME type: application/javascript

  • When the use of data is predefined, this type is used.
  • Mostly used when the data is of type JSON-P or javascript object notation with API wrapped in a function call.
  • The function will be defined in the client-side js code while the API is passed as a parameter that wholly acts as a javascript code.

Syntax

Java:

response.setHeader("Accept", "application/javascript");
response.setHeader("Content-type", "application/javascript");

Conclusion

  • 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.
Tags
Submitted by arilio666 on January 13, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.