Created
November 18, 2020 17:16
-
-
Save addeeandra/7d652a5aefcab606002539b0e2d4144e to your computer and use it in GitHub Desktop.
A snippet code to build MultipartBody.Part easily.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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