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

The Content-Type HTTP header is used to indicate the type of media in the body of the message. The official MIME type for JSON (JavaScript Object Notation) is application/json, and the default encoding is UTF-8. 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. Some legacy content types for JSON should be avoided (text/json, text/x-json, text/javascript, etc.). Click Send to execute the JSON Content-Type request online and see the results. The Java code was automatically generated for the JSON Content Type example.
What is the correct Content Type for JSON? [Java 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: 10960 times
Java code for JSON Content Type example

Java code for JSON Content Type Example

This Java 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 Java 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 Java code generator.