Correct Content-Type Header for JSON [PHP Code]

The correct content type for JSON is application/json, and the correct content type for JSONP (padded JSON) is application/javascript. For JSON-LD (JSON-linked data), the correct content type is application/ld+json. Legacy JSON content types, such as text/json, text/x-json, and text/javascript, should be avoided. The Content-Type HTTP header is used to indicate the type of media in the body of the HTTP message. The default encoding for JSON (JavaScript Object Notation) is UTF-8. In this PHP JSON Content-Type example, we send JSON to the ReqBin echo URL with an application/json Content-Type header. Click Send to execute the PHP JSON Content-Type request online and see the results. The PHP code was automatically generated for the JSON Content Type example.
Correct Content-Type Header 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: 49965 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

What is JSON?

JSON (JavaScript Object Notation) is a text format for representing structured data based on the syntax of the JavaScript language. The .json extension is used to identify JSON files. There are many programming languages that use JSON to exchange data, including Python, Java, JavaScript, PHP, C++, C#, Go, and many more.

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

What is Content Type?

The Content-Type header indicates the type of media the resource uses in an HTTP entity. A content type is specified according to MIME (Multipurpose Email Extensions), which are standardized and published by the Internet Assigned Numbers Authority (IANA). The Content-Type header describes the nature of data in the body of HTTP messages by identifying type identifiers, subtype identifiers, and optional parameters.

Content-Type Example
Content-Type: image/png
Content-Type: text/html; charset=UTF-8
Content-Type: multipart/form-data;

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

Each resource transmitted over HTTP has a media type, also known as a MIME type, that describes the resource type and allows browsers and servers to understand it properly. As an example, if the server is capable of accepting both JSON and XML content types on one endpoint, a Content-Type of application/json will tell the server to interpret the body data as JSON, whereas a Content-Type of application/xml will tell the server to interpret the body data as XML.

Content-Type Header for JSON Example
Content-Type: application/json

How to send JSON with correct Content-Type header?

The following is an example of posting JSON data to the ReqBin echo URL:

POST 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
}

How to get JSON with correct Content-Type header?

The following is an example of loading JSON data from the ReqBin echo URL:

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

{"success":"true"}

See also

Generate code snippets for PHP and other programming languages

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