com.lightningkite.rx.okhttp.HttpBody.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of okhttp-jackson Show documentation
Show all versions of okhttp-jackson Show documentation
An OkHttp wrapper based on RxJava using Jackson for JSON
The newest version!
package com.lightningkite.rx.okhttp
import io.reactivex.rxjava3.core.Single
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import okio.BufferedSink
import okio.Okio
import java.io.ByteArrayOutputStream
import java.io.File
/**
* Transforms a given object into a JSON request body using the [defaultJsonMapper].
*/
fun Any?.toJsonRequestBody(): RequestBody {
val sending = defaultJsonMapper.writeValueAsString(this)
return sending.toRequestBody(MediaType.JSON)
}
/**
* A shortcut for using the [MultipartBody.Builder].
*/
fun MultipartBody.Companion.from(vararg parts: MultipartBody.Part): RequestBody = from(parts.toList())
/**
* A shortcut for using the [MultipartBody.Builder].
*/
fun MultipartBody.Companion.from(parts: List): RequestBody {
return MultipartBody.Builder().setType(MultipartBody.FORM).also {
for (part in parts) {
it.addPart(part)
}
}.build()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy