📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Maven Dependency
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
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\" : \"ramesh@gmail.com\"\r\n" +
"}";
String response = example.post("http://localhost:8080/api/v1/employees", json);
System.out.println(response);
}
}
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);
}
}
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\" : \"ram@gmail.com\"\r\n" +
"}";
String response = example.post("http://localhost:8080/api/v1/employees/1", json);
System.out.println(response);
}
}
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);
}
}
Comments
Post a Comment
Leave Comment