Sending Request to JSON API [Java Code]

To send a request to a JSON API endpoint, you need to make an HTTP API request and indicate that your client can accept the JSON API response by passing the "Accept: application/vnd.api+json" HTTP header to the server. The server informs the client that it has returned a JSON API response by providing the "Content-Type: application/vnd.api+json" HTTP header. When sending data to a JSON API endpoint, you must include a single resource object in the POST message, and the object must contain at least a type attribute. In this JSON API example, we send data to the ReqBin JSON API echo UR and provide the "Accept: application/vnd.api+json" and "Content-Type: application/vnd.api+json" HTTP headers to the server. Click Send to execute the JSON API Example online and see the results. The Java code was automatically generated for the JSON API example.
Sending Request to JSON API [Java Code] Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
Content-Length: 256

{
  "data": {
    "type": "photos",
    "attributes": {
      "title": "John Smith",
      "src": "//reqbin.com/images/john-smith.png"
    },
    "relationships": {
      "photographer": {
        "data": { "type": "people", "id": "1" }
      }
    }
  }
}
Updated: Viewed: 53139 times
Java code for JSON API example

Java code for JSON API Example

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

What is JSON?

JavaScript Object Notation (JSON) is a standard textual format for storing and transmitting data over a network. JSON is used in almost all scripting languages, including JavaScript, Python, Java, C++, C#, Go, PHP, and many others. JSON is widely used for client/server communication, transferring data to and from the server in web and mobile applications.

What is API?

Application Programming Interface (API) is a software intermediary for creating and integrating application software that allows two applications to exchange data. APIs conform to REST and HTTP standards, are easy to use and are developer-friendly. The APIs are well documented for use and version control. The API has its software development lifecycle (SDLC), which includes designing, testing, building, managing, and versioning, just like any other software produced.

What is JSON API?

JSON API is a text-based data exchange format designed to work with HTTP. The JSON API defines how clients should request data from the server and how the server should respond to requests. The JSON API provides improved caching functionality and optimizes HTTP requests by eliminating unnecessary server requests. JSON API is described at JSONAPI.org and differs from REST API.

JSON API: what are the benefits?

The JSON API has the main advantage of reducing both client and server requests and data transfers between clients and servers.

How to send a JSON API Request?

To send a JSON API request, clients and servers must send requests and responses with the following HTTP header:

Content-Type Header Example
Content-Type: application/vnd.api+json

JSON API clients must also send an Accept header to tell the server that they can accept JSON API requests.

Accept Header Example
Accept: application/vnd.api+json

JSON API Request Example

The following is an example of sending a JSON API request that we send to the echo ReqBin URL:

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

See also

Generate code snippets for Java and other programming languages

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