Convert curl to Kotlin

curl2code converts curl commands to Kotlin code using OkHttp. The conversion runs in your browser via WebAssembly for complete privacy. For Java, check our curl to Java converter. For Swift (iOS), see curl to Swift. Below are practical Kotlin examples.

How to copy curl from your browser

  1. 1

    Open DevTools

    Press F12 or Ctrl+Shift+I to open your browser developer tools.

  2. 2

    Go to the Network tab

    Click the Network tab and perform the action that triggers the HTTP request.

  3. 3

    Copy as cURL

    Right-click the request → CopyCopy as cURL. Then paste it above.

Frequently Asked Questions

What is Kotlin OkHttp?

OkHttp is a widely used HTTP client for Kotlin and Java, developed by Square. It features connection pooling, transparent GZIP, response caching, and HTTP/2 support. Add with implementation("com.squareup.okhttp3:okhttp:4.x"). It's the default library curl2code uses for Kotlin conversions and is the standard HTTP client for Android.

OkHttp vs Ktor Client vs Fuel — which to use?

OkHttp is the industry standard, especially for Android. Ktor Client is Kotlin-first with coroutine support and multiplatform capability. Fuel is lightweight with a clean Kotlin DSL. For Android, OkHttp (often via Retrofit) is the default choice. For JVM comparison, see our curl to Java converter.

How to handle authentication in Kotlin?

Add a header: request.addHeader("Authorization", "Bearer $token"). For Basic auth: use Credentials.basic(user, pass) utility. OkHttp also supports Authenticator interface for automatic retry with credentials on 401 responses. curl2code handles -u and Bearer flags automatically.

How to send multipart form data in Kotlin?

Use OkHttp's MultipartBody.Builder(): .addFormDataPart("file", filename, fileBody) for files and .addFormDataPart("key", "value") for fields. Set .setType(MultipartBody.FORM). curl2code converts -F flags to the correct OkHttp multipart builder code.

How to handle HTTP errors in Kotlin?

Check response.isSuccessful for 2xx status or read response.code. OkHttp throws IOException on network failures. Use Kotlin's runCatching { } or try/catch for clean error handling. Always close the response body with .use { } block to prevent resource leaks.

How to use coroutines with OkHttp?

Wrap blocking OkHttp calls in withContext(Dispatchers.IO) { client.newCall(request).execute() }. Or use the suspendCancellableCoroutine extension with enqueue() for true async. Ktor Client has native coroutine support built-in. For similar async patterns, see curl to Swift (async/await).

How to set timeouts in Kotlin OkHttp?

Configure on the client: OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS).readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).build(). For retries, add an interceptor that retries on failure. These map to curl's --connect-timeout and --max-time.

How to use a proxy with OkHttp?

Set on the client builder: OkHttpClient.Builder().proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress("proxy", 8080))).build(). For authentication, add proxyAuthenticator. OkHttp also supports SOCKS proxies with Proxy.Type.SOCKS. curl2code converts -x flags to this configuration.

How to send a POST request with JSON body in Kotlin?

Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d "{"key":"value"}" URL to Kotlin using curl2code. The generated code uses RequestBody.create(MediaType.parse("application/json"), json). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the OkHttp code.

How to add Bearer token authorization in Kotlin?

Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates OkHttp code with the auth header: .addHeader("Authorization", "Bearer YOUR_TOKEN"). curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.

How to set Content-Type header in Kotlin?

curl2code converts curl -H "Content-Type: application/json" URL to OkHttp code with the proper header: .addHeader("Content-Type", "application/json"). For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct OkHttp API.

Useful Links

curl Guides

Convert curl to Other Languages