📘 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.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare 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);
}
}
Basic POST Request Example
package com.javaguides.okhttp.tutorial;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttpPostRequestParameterExample {
private static final String BASE_URL = "http://localhost:8080/spring-rest";
static OkHttpClient client = new OkHttpClient();
public static void main(String[] args) throws IOException {
final RequestBody formBody = new FormBody.Builder()
.add("username", "test")
.add("password", "test").build();
final Request request = new Request.Builder()
.url(BASE_URL + "/users")
.post(formBody).build();
final Call call = client.newCall(request);
final Response response = call.execute();
System.out.println(response);
}
}
POST Request with JSON Example
package com.javaguides.okhttp.tutorial;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttpPostExample {
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();
}
}
String bowlingJson(String player1, String player2) {
return "{'winCondition':'HIGH_SCORE'," + "'name':'Bowling'," + "'round':4," + "'lastSaved':1367702411696," +
"'dateStarted':1367702378785," + "'players':[" + "{'name':'" + player1 +
"','history':[10,8,6,7,8],'color':-13388315,'total':39}," + "{'name':'" + player2 +
"','history':[6,10,5,10,10],'color':-48060,'total':41}" + "]}";
}
public static void main(String[] args) throws IOException {
OkHttpPostExample example = new OkHttpPostExample();
String json = example.bowlingJson("Jesse", "Jake");
String response = example.post("http://www.roundsapp.com/post", json);
System.out.println(response);
}
}
http://www.roundsapp.com/393141003
Multipart POST Request
package com.javaguides.okhttp.tutorial;
import java.io.File;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class OkHttpPostUploadFileExample {
private static final String BASE_URL = "http://localhost:8080/spring-rest";
static OkHttpClient client = new OkHttpClient();
public static void main(String[] args) throws IOException {
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("username", "test").addFormDataPart("password", "test")
.addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"),
new File("src/test/resources/test.txt")))
.build();
Request request = new Request.Builder().url(BASE_URL + "/users/multipart").post(requestBody).build();
Call call = client.newCall(request);
Response response = call.execute();
System.out.println(response.code());
}
}
Comments
Post a Comment
Leave Comment