Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty HTTP Get response body when okhttp3 is used

I'm trying to make GET request to some url - as result status response is 200 but response body is very strange - okhttp3.internal.http.RealResponseBody@84f1f6e. I have this result for any url("https://www.bbc.com/russia" is for example). Below is snapshot of my code:

val httpClient: OkHttpClient = OkHttpClient.Builder()
    .addInterceptor(LoggingInterceptor())
    .addNetworkInterceptor(StethoInterceptor())
    .build()


fun runCheckQualityPeriodic() {
    val request = Request.Builder()
        .url("https://www.bbc.com/russia")
        .build()

    val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
    StrictMode.setThreadPolicy(policy)

    val response = httpClient.newCall(request).execute()
    Logger.tag("krv").v("runCheckQualityPeriodic responce is " + response)
    val body = response.body().toString()
    Logger.tag("krv").v("runCheckQualityPeriodic responce body is " + body)
}

Below are screenshots from StethoInterceptor:

enter image description here Do you have any idea why I receive such result?

like image 912
Mikola Avatar asked Feb 01 '26 19:02

Mikola


1 Answers

toString() shows a developer-friendly representation. To get the actual content you need to change

response.body().toString()

to

response.body().string()
like image 91
xjcl Avatar answered Feb 03 '26 08:02

xjcl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!