Friday, October 24, 2025
HomeLanguagesWhat is the correct JSON content type ?

What is the correct JSON content type ?

Content-Type is an HTTP header that is used to indicate the media type of the resource and in the case of responses, it tells the browser about what actually content type of the returned content is. In case of any POST or PUT requests, the client tells the server about the kind of data sent.
To know about the type of content the browser is going to encounter, it does a MIME sniffing. MIME or Multipurpose Internet Mail Extension is a specification for non-text e-mail attachments. It allows the mail client or Web browser to send and receive different file formats as an attachment over the Email. For receiving a JSON request, it is important to mention or tell the browser about the type of request it is going to receive. So we set its MIME type by mentioning it in the Content-Type. We can do the same in two ways:  

  • MIME type: application/json
  • MIME type: application/javascript

MIME type: application/json 
It is used when it is not known how this data will be used. When the information is to be just extracted from the server in JSON format, it may be through a link or from any file, in that case, it is used. In this, the client-side only gets the data in JSON format that can be used as a link to data and can be formatted in real-time by any front end framework. 
 

  • Example: In this example, the MIME-type is application/json as it is just extracting the dictionary from that variable and putting it in JSON format, and showing it. 
     

php




<?php
// Setting the header
header('Content-Type:application/json');
 
// Initializing the directory
$dir =[
    ['Id'=> 1, 'Name' => 'Geeks' ],
    ['Id'=> 2, 'Name' => 'for'],
    ['Id'=> 3, 'Name' => 'Geeks'],
      ];
// Shows the json data
echo json_encode($dir);
?>


  • Output: 
     
[{"Id":1, "Name":"Geeks"}, {"Id":2, "Name":"for"}, {"Id":3, "Name":"Geeks"}]

MIME type: application/javascript 
It is used when the use of the data is predefined. It is used by applications in which there are calls by the client-side ajax applications. It is used when the data is of type JSON-P or JSONP. JSONP or JavaScript Object Notation with Padding is used when the API is wrapped in a function call. The function is defined in the client-side JavaScript code and the API is passed to it as a parameter and thus it acts as executable JavaScript code.
 

  • Example: In this example, the MIME-type is application/javascript as it is just extracting the dictionary from a variable, extract it in JSON format and then send it as a parameter to a function call at a client-side. 
     

php




<?php
 
// Using application/javascript
header('Content-Type:application/javascript');
$dir =[
    ['Id'=> 1, 'Name' => 'Geeks' ],
    ['Id'=> 2, 'Name' => 'for'],
    ['Id'=> 3, 'Name' => 'Geeks'],
      ];
 
// Making a function call to the client side 
// using Function_call()
// Sending JSON data as a parameter to client.
echo "Function_call(".json_encode($dir).");";
 
?>


  • Output: 
     
Function_call([{"Id":1, "Name":"Geeks"}, {"Id":2, "Name":"for"}, 
{"Id":3, "Name":"Geeks"}])

It is recommended to use application/json instead of application/javascript because the JSON data is not considered as a javascript code. It is a standard and thus is given a separate content type as i.e. application/json. 
 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS