[kotlin] Fix empty body with POST/etc in OkHttp client (#13666)

This commit is contained in:
Segev Finer
2022-10-11 14:18:24 +03:00
committed by GitHub
parent 1e66f2fbc9
commit 53873ff6d8
21 changed files with 168 additions and 168 deletions

View File

@@ -156,7 +156,7 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}.build()
// take content-type/accept from spec or set to default (application/json) if not defined
if (requestConfig.headers[ContentType].isNullOrEmpty()) {
if (requestConfig.body != null && requestConfig.headers[ContentType].isNullOrEmpty()) {
requestConfig.headers[ContentType] = JsonMediaType
}
if (requestConfig.headers[Accept].isNullOrEmpty()) {
@@ -164,16 +164,16 @@ open class ApiClient(val baseUrl: String, val client: OkHttpClient = defaultClie
}
val headers = requestConfig.headers
if(headers[ContentType].isNullOrEmpty()) {
throw kotlin.IllegalStateException("Missing Content-Type header. This is required.")
}
if(headers[Accept].isNullOrEmpty()) {
if (headers[Accept].isNullOrEmpty()) {
throw kotlin.IllegalStateException("Missing Accept header. This is required.")
}
// TODO: support multiple contentType options here.
val contentType = (headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
val contentType = if (headers[ContentType] != null) {
// TODO: support multiple contentType options here.
(headers[ContentType] as String).substringBefore(";").lowercase(Locale.getDefault())
} else {
null
}
val request = when (requestConfig.method) {
RequestMethod.DELETE -> Request.Builder().url(url).delete(requestBody(requestConfig.body, contentType))