Skip to content

Instantly share code, notes, and snippets.

@addeeandra
Created November 18, 2020 17:16
Show Gist options
  • Save addeeandra/7d652a5aefcab606002539b0e2d4144e to your computer and use it in GitHub Desktop.
Save addeeandra/7d652a5aefcab606002539b0e2d4144e to your computer and use it in GitHub Desktop.
A snippet code to build MultipartBody.Part easily.
object PartRequest {
@JvmStatic
fun buildFileBody(file: File): RequestBody {
return RequestBody.create(MediaType.parse("image/jpg"), file)
}
@JvmStatic
fun buildTextBody(value: String): RequestBody {
return RequestBody.create(MediaType.parse("text/plain"), value)
}
@JvmStatic
fun buildFilePart(file: File, name: String = "img"): MultipartBody.Part {
val requestBody = RequestBody.create(MediaType.parse("image/jpg"), file)
return MultipartBody.Part.createFormData(name, file.name, requestBody)
}
@JvmStatic
fun buildTextPart(name: String, value: String): MultipartBody.Part {
return MultipartBody.Part.createFormData(name, value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment