What is the correct Content Type for JSON? [PHP Code]

The official MIME type for JSON (JavaScript Object Notation) is application/json, and the default encoding is UTF-8. For JSONP (padded JSON), the correct content type is application/javascript. For JSON-LD (JSON linked data), the correct content type is application/ld+json. There are some legacy content types for JSON that should be avoided (text/json, text/x-json, text/javascript, etc.). The PHP code was automatically generated for the JSON Content Type example.
What is the correct Content Type for JSON? [PHP Code] Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 81

{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}
Updated: Viewed: 8506 times
PHP code for JSON Content Type example

PHP code for JSON Content Type Example

This PHP code snippet was generated automatically for the JSON Content Type example.
<< Back to the JSON Content Type example

Why is it important to specify the correct content type for JSON?

Every resource transmitted over HTTP has a media type, also known as a MIME type, which is used to describe the resource type in the body of an HTTP message and allows browsers and servers to interpret them correctly. For example, if the server can accept JSON and XML content types on the same endpoint, then setting Content-Type to application/json will tell the server to interpret the data in the body as JSON, and setting Content-Type to application/xml will tell the server to interpret the data as XML.

Content-Type: application/json

Example of sending JSON with correct Content-Type header

Example of posting JSON data to the ReqBin echo URL.

JSON Request Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 81

{
  "Id": 78912,
  "Customer": "Jason Sweet",
  "Quantity": 1,
  "Price": 18.00
}

Example of getting JSON with correct Content-Type header

An example of loading JSON data from the ReqBin echo URL.

JSON Response Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19

{"success":"true"}

See also JSON Request Example, JSON Response Example.

Generate code snippets for PHP and other programming languages

Convert your JSON Content Type request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.