0

I implementing the OkHttpClient in my service. When I hit 150+ connections in a connectionPool in the okHttpClient at a time, the below exception has been thrown. Any one aware of this exception. How to fix this issue ? Is that any problem in GZiping ?

Error :

ResponseCode: 500 INTERNAL_SERVER_ERROR, ErrorMessage:ca.uhn.fhir.parser.DataFormatException: Failed to parse JSON encoded API content: Content does not appear to be API JSON, first non-whitespace character was: '<' (must be '{')

Code snippet :

private fun okhttpioConnection(): String {

    log.info("Initializing OKHttp-ioConnection")
    client = OkHttpClient()
    client.setConnectTimeout(600000, TimeUnit.MILLISECONDS)
    client.setReadTimeout(600000, TimeUnit.MILLISECONDS)
    client.connectionPool = ConnectionPool(300, 20000)
    client.retryOnConnectionFailure = true

    var request = createRequest(restParams) // GET or POST
    var response = client.newCall(request).execute()
    var responseCodeString = response.code().toString()
    return response.body()!!.string()

}
fun createRequest(restParams: RestParams): Request {
    if (restParams.method == "POST") {
      return Request.Builder()
        .post(RequestBody.create(
          MediaType.parse("application/json; charset=utf-8"), restParams.body.toString()))
        .header("Authorization", "xxxxxx")
        .addHeader("Content-Length",restParams.body.length.toString())
        .addHeader("Accept", "application/json, text/json")
        .url(restParams.url)
        .build()
    } else {
    return Request.Builder()
      .header("Authorization", "xxxxxxx")
      .addHeader("Accept", "application/json, text/json")
      .url(restParams.url).get()
      .build()
    }
  }```
Kaushik KR
  • 11
  • 2

1 Answers1

0

I resolved this issue. According to my analysis, OkHttpClient cannot throw the exception explicitly. This way, we can throw an exception manually by checking the http status code of the request. I missed to configure some http status code in my services. So that it happens.

Is there any way to automatically throw an exception in the OkHttpClient ?

Kaushik KR
  • 11
  • 2