Sending JSON Payload to the Server [Java Code]

To send the JSON payload to the server using Java, you need to enclose the JSON data in the HTTP request body and indicate the data type of the request body with the "Content-Type: application/json" request header. If the client expects a response from the server in JSON format, it also needs to send the "Accept: application/json" header to the server. In this Java JSON Payload Example, we send JSON to the ReqBin echo URL. Click Send to execute the Java JSON Payload request online and see the results. The Java code was automatically generated for the JSON Payload example.
Sending JSON Payload to the Server [Java Code] Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 52

{
  "Id": 78912,
  "Quantity": 1,
  "Price": 18.00
}
Updated: Viewed: 66441 times
Java code for JSON Payload example

Java code for JSON Payload Example

This Java code snippet was generated automatically for the JSON Payload example.
<< Back to the JSON Payload example

What is JSON?

JavaScript Object Notation (JSON) is a standard text format for storing and transmitting data over a network. API clients use the JSON format extensively to send or receive data from the server. JSON represents four primitive types (strings, numbers, boolean values, and null) and two structured types (objects and arrays). JSON file names use the .json file extension.

What is JSON Payload?

The request payload is important information in a data block that clients send to the server in the body of an HTTP POST, PUT or PATCH message that contains important information about the request. The Payload can be sent or received in various formats, including JSON. In programming, the payload is used in the context of a message protocol to distinguish between ancillary and actual data in a request. For example, request headers are required by the server to interpret and process the request from a client correctly, but they are not the request's payload. JSON syntax elements are also not a payload.

JSON Payload Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 52

{
  "Id": 12345
}

Where:
  • "id": "12345" - Payload
  • The rest of the information is called service data.

API Payload Formats

The following are the two types of payload:

The Payload that the client sends to the server
Request Payload Format
{
"Id": 78912,
"Customer": "Jason Sweet"
}

Where:
  • "Id": 78912 and "Customer": "Jason Sweet" are Payload
The Payload returned by the server
1. The OK response Payload format:
OK Payload Example
{
  "success": "true",
  "data": {
    "TotalOrders": 100
  }
}

Where:
  • "success": "true" and "TotalOrders": 100 are Payload
2. The FAILED response payload format:
FAILED Payload Example
{
  "success": "false",
  "errorCode": 500,
  "errorDescription": Invalid user ID" 
}

Where:
  • "success": "false", "errorCode": 500 and "errorDescription": Invalid user ID" are Payload

JSON Payload Request Example

Example of sending JSON Payload to ReqBin echo URL:

JSON Payload Request Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 52
      
{
  "Id": 78912,
  "Quantity": 1,
  "Price": 18.00
}

JSON Payload Response Example

Example of server response with JSON Payload:

JSON Payload 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 Payload request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Java code generator.