Skip to content

Instantly share code, notes, and snippets.

@balvinder294
Created November 24, 2019 14:27
Show Gist options
  • Save balvinder294/e869944161cb0af250b1296f64e3129a to your computer and use it in GitHub Desktop.
Save balvinder294/e869944161cb0af250b1296f64e3129a to your computer and use it in GitHub Desktop.
Sample method for posting a multipart file to rest api with OkHttp
/**********
* Here is a method showing use of OkHttp for posting a multipart form data
*
* Here RPResumeResponseDTO is a custom DTO being used in the project
* For taking file from rest api endpoint we are using as a multipart file parameter
**********/
/**************Steps *******************/
RPResumeResponseDTO postResumeToRPServer(MultipartFile file) throws IOException {
log.debug("Request to post file : {}",file.getOriginalFilename());
/ * Use your Custom object to handle data if any in place of */
RPResumeResponseDTO resumeResponseDTO = new RPResumeResponseDTO();
/*** Step 1*********************
** Initialize OkHttp Client****
******************************/
OkHttpClient okHttpClient = new OkHttpClient()
.newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.build();
/*******Step 2************************************************************
** Create a Multipart Body to handle data params and file we need to send **
***************************************************************************/
// A request body to append file
RequestBody fileBody = RequestBody.create(okhttp3.MediaType.parse(file.getContentType()), file.getBytes());
MultipartBody multipartBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM) // Header to show we are sending a Multipart Form Data
.addFormDataPart("resumeFile", file.getOriginalFilename(),fileBody) // file param
.addFormDataPart("someText", "textValue") // other string params can be like userId, name or something
.build();
/*******Step 3************************************************************
** Create Request to call the api passing api url**
***************************************************************************/
Request request = new Request.Builder()
.url(apiEndpoint) // pass the url endpoint of api
.post(multipartBody) // pass the mulipart object we just created having data
.build();
/*******Step 4************************************************************
** Execute the api and create a Response Object to handle response**
***************************************************************************/
Response resp = okHttpClient.newCall(request).execute();
/*******Step 5************************************************************
** Handling data from Response**
***************************************************************************/
String response = resp.body().string();
if(response) {
/* DO something with your response here or parse response to a object */
// For mapping the response to your custom class
ObjectMapper objectMapper = new ObjectMapper();
resumeResponseDTO = objectMapper.readValue(response, RPResumeResponseDTO.class);
}
resp.close(); // Close response
return resumeResponseDTO;
}
@Quireg
Copy link

Quireg commented Feb 3, 2024

Thank you kindly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment