rysv
rysv

Reputation: 3460

OkHttpClient POST request fails when Request body size beyond certain limit

I am making a sequence of POST requests with each subsequent request passing the next token received the previous response in the request body of the new request. I am noticing that if POST HTTP request body size is beyond a certain limit, the request times out after certain number of requests in the sequence. Typical request body size is about 14KB. With OkHttpClient event listener I notice that (for the failing call)

override fun requestBodyStart(call: Call)

is invoked but not

override fun requestBodyEnd(call: Call, byteCount: Long)

Instead override fun requestFailed(call: Call, ioe: IOException) is called with Socket timeout exception.

It works if make this call before each request (but looks like a hack)

okHttpClient.connectionPool.evictAll()

Not sure if OkHTTPClient maintains some state within. Charles is showing Failure status with IO: Stream cancelled by CLIENT message.

I am using

implementation "com.squareup.okhttp3:okhttp:4.9.3"

Backend is not in my control and I cannot optimize the request body size. Apparently, a truncated-body request reaches server which doesn't response and hence the timeout. Is there a way to fix this issue. Note: If I replace OkHttp with Volley I do not see this issue.

Upvotes: 0

Views: 932

Answers (1)

Muhammad Saad
Muhammad Saad

Reputation: 733

If I understand correctly, you are facing timeout issue because of the response. you can configure the OkHttpClient.

const val CONNECT_TIMEOUT = 10L
const val READ_TIMEOUT = 10L

val client = OkHttpClient.Builder().connectTimeout(CONNECT_TIMEOUT, TimeUnit.MINUTES)
            .readTimeout(READ_TIMEOUT, TimeUnit.MINUTES).build()

This will solve your problem. It will increase the time of your request and you will get response within the given time.

Upvotes: 0

Related Questions