OkHttp for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is OkHttp and why do you need it?

\n\n

OkHttp is a powerful and efficient HTTP library for JVM (Java Virtual Machine), Android, and Kotlin/Native, developed by Square. It provides a convenient API for making HTTP requests and processing responses. OkHttp is the de facto standard for networking in the Android ecosystem and is actively used in server-side Kotlin applications.

\n\n

Why use OkHttp instead of standard Java tools (HttpURLConnection)?

\n
    \n
  • Simplicity and convenience: A clean, fluent API that is easy to read and write.
  • \n
  • Performance: Built-in support for HTTP/2, automatic response compression (gzip), connection pooling, and caching.
  • \n
  • Reliability: Automatic request retries on network failures, resilience to DNS and TLS errors.
  • \n
  • Flexibility: Easily configurable interceptors for logging, authentication, modifying requests and responses.
  • \n
  • Testability: Ability to mock the network layer using MockWebServer.
  • \n
\n\n

OkHttp is ideal for Android applications, REST API clients, microservices, and any other Kotlin projects that require reliable and fast HTTP communication.

\n\n

Installation

\n\n

OkHttp is distributed via Maven Central. To add it to your project, use the Gradle build system (most common for Kotlin).

\n\n

For Android / Kotlin/JVM project (build.gradle.kts):

\n
dependencies {\n    implementation("com.squareup.okhttp3:okhttp:4.12.0")\n    // For logging (optional)\n    implementation("com.squareup.okhttp3:logging-interceptor:4.12.0")\n}
\n\n

For Android / Kotlin/JVM project (build.gradle):

\n
dependencies {\n    implementation 'com.squareup.okhttp3:okhttp:4.12.0'\n    implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'\n}
\n\n

For Maven (pom.xml):

\n
<dependency>\n    <groupId>com.squareup.okhttp3</groupId>\n    <artifactId>okhttp</artifactId>\n    <version>4.12.0</version>\n</dependency>
\n\n

Important: Make sure the OkHttp version is compatible with your Kotlin version. The latest version can always be found on MVNRepository.

\n\n

Quick Start

\n\n

The simplest example is to make a GET request and get the response body as a string.

\n\n
import okhttp3.OkHttpClient\nimport okhttp3.Request\nimport java.io.IOException\n\nfun main() {\n    // 1. Create a client (usually one per application)\n    val client = OkHttpClient()\n\n    // 2. Build the request\n    val request = Request.Builder()\n        .url("https://api.github.com/users/octocat")\n        .build()\n\n    // 3. Execute the request synchronously (blocks the current thread)\n    client.newCall(request).execute().use { response ->\n        if (!response.isSuccessful) {\n            throw IOException("Unexpected code $response")\n        }\n        // 4. Get the response body\n        println(response.body?.string())\n    }\n}
\n\n

Explanation:

\n
    \n
  1. OkHttpClient() — creates a client instance. It is thread-safe, so it is recommended to create one instance and reuse it.
  2. \n
  3. Request.Builder() — uses the Builder pattern to configure the request.
  4. \n
  5. client.newCall(request) — creates a new Call, which can be executed synchronously (execute()) or asynchronously (enqueue()).
  6. \n
  7. response.use { ... } — ensures that response resources (body, connection) are closed after use.
  8. \n
  9. response.body?.string

Recommendations