Java Rest Client using OkHttp - GET, POST, PUT and DELETE Request Examples

In this post, we will create a Rest Client using OkHttp library. Let's create a Rest Client for GET, POST, PUT and DELETE HTTP request in Java.
OkHTTP is an open source project designed to be an efficient HTTP client for Android and Java applications.
OkHttp supports Android 5.0+ (API level 21+) and Java 1.8+. In this article, we will write a code using Java 1.8+.

Maven Dependency

Let’s first add the library as a dependency into the pom.xml:
<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.9.0</version>
</dependency>
To see the latest dependency of this library check out the page on Maven Central.
In this post, we will create a Rest Client for spring boot CRUD example project. This spring boot crud example project is deployed and up and running. Refer to this article - https://www.javaguides.net/2018/09/spring-boot-2-hibernate-5-mysql-crud-rest-api-tutorial.html
Let's first do the POST request to insert user object into a database and then we will do GET, PUT and DELETE requests.

OkHttp POST Request Java Example

package com.javaguides.okhttp.tutorial.crud;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpPost {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    OkHttpClient client = new OkHttpClient();

    String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(url).post(body).build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) throws IOException {
        OkHttpPost example = new OkHttpPost();
        String json = "{\r\n" +
            " \"firstName\" : \"Ramesh\",\r\n" +
            " \"lastName\" : \"Fadatare\",\r\n" +
            " \"emailId\" : \"[email protected]\"\r\n" +
            "}";
        String response = example.post("http://localhost:8080/api/v1/employees", json);
        System.out.println(response);
    }
}
Below diagram shows the screenshot of source code as well as output:

OkHttp GET Request Java Example

package com.javaguides.okhttp.tutorial.crud;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpGet {

    OkHttpClient client = new OkHttpClient();

    public String run(String url) throws IOException {
        Request request = new Request.Builder().url(url).build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) throws IOException {
        OkHttpGet example = new OkHttpGet();
        String response = example.run("http://localhost:8080/api/v1/employees/1");
        System.out.println(response);
    }
}
Below diagram shows the screenshot of source code as well as output: 

OkHttp PUT Request Java Example

package com.javaguides.okhttp.tutorial.crud;

import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpPut {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    OkHttpClient client = new OkHttpClient();

    String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(url).put(body).build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) throws IOException {
        OkHttpPut example = new OkHttpPut();
        String json = "{\r\n" +
            " \"firstName\" : \"Ram\",\r\n" +
            " \"lastName\" : \"Fadatare\",\r\n" +
            " \"emailId\" : \"[email protected]\"\r\n" +
            "}";
        String response = example.post("http://localhost:8080/api/v1/employees/1", json);
        System.out.println(response);
    }
}
Below diagram shows the screenshot of source code as well as output: 

OkHttp DELETE Request Java Example

package com.javaguides.okhttp.tutorial.crud;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class OkHttpDelete {

    OkHttpClient client = new OkHttpClient();

    public String run(String url) throws IOException {
        Request request = new Request.Builder().url(url).delete().build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public static void main(String[] args) throws IOException {
        OkHttpDelete example = new OkHttpDelete();
        String response = example.run("http://localhost:8080/api/v1/employees/1");
        System.out.println(response);
    }
}
Below diagram shows the screenshot of source code as well as output: 

Comments