ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
13k stars 1.06k forks source link

Ktor Clients changing request body-type on Android 4.4.2 - 5.0.1 #1013

Closed nbaklanov-issart closed 5 years ago

nbaklanov-issart commented 5 years ago

Ktor Version

1.0.1

Ktor Engine Used(client or server and name)

Ktor Clients.

io.ktor:ktor-client:$ktor_version
io.ktor:ktor-client-android:$ktor_version
io.ktor:ktor-client-ios:$ktor_version

Creating client:

protected val client = HttpClient {
        expectSuccess = false
    }

JVM Version, Operating System and Relevant Context

Android 4.4.2 - 5.0.1

Feedback

Hello, everyone. I am working on the mobile multiplatform project and using .ktor on client side (on Android phones).

I have following code to send POST requests :

protected val client = HttpClient {
        expectSuccess = false
    }
...
protected fun sendPostRequest(requestBody:Any = EmptyContent,
                                  parameters:List<Pair<String, String>> = emptyList(),
                                  requestHeaders:List<Pair<String, String>> = emptyList()) {
        GlobalScope.launch(CoroutineContextProvider.provideContext()) {
            try {
                val call = client.request<HttpResponse> {
                    url{
                        protocol = URLProtocol.HTTP
                        host = serverAddress
                        encodedPath = requestEncodedPath
                    }
                    method = HttpMethod.Post
                    body = requestBody

                    for (currentPair:Pair<String, String> in requestHeaders) {
                        headers.append(currentPair.first, currentPair.second)
                    }
                    for (currentPair:Pair<String, String> in parameters) {
                        parameter(currentPair.first, currentPair.second)
                    }
                }
                requestResponseHandler(call, call.readText())
            } catch (requestException:Exception) {
                requestExceptionHandler(requestException)
            }
        }
    }

And I calling this function passing body with json type as parameter :

val body:Any = TextContent(loginData.toString(), contentType = ContentType.Application.Json)
        sendPostRequest(body)

Everything working fine, but on Android 4.4.2 content-type of the request is changed to “text/html; charset=iso-8859-1” (according to proxy logs).

I am doing something wrong, or it’s natural behaviour of this Android version?

Thanks.

nbaklanov-issart commented 5 years ago

I checked this once again and found that body-type wasn't change it was a bug in proxy program. It displayed wrong value for body-type.

I didn't found a real reason why I was getting those errors (maybe because of some server logic), but when I changed engine for android from Android to OkHttp problem disappeared.

e5l commented 5 years ago

Hi @nbaklanov-issart, the additional report was added in 1.2.0.