What is the correct JSON content type ?

Content-Type is an HTTP header which is used to indicate the media type of the resource and in 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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);
    ?>

    chevron_right

    
    

  • 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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).");";
      
    ?>

    chevron_right

    
    

  • 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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.