curl to Kotlin
Convert curl commands to Kotlin OkHttp code. Supports headers, POST data, auth, cookies, and common flags.
Presets:
Paste your raw curl request command here. Safe local storage is used.
Kotlin code updated. Method: POST, URL host: api.example.com.
Target Host
api.example.com
HTTP Method
POST
Headers Detected
2
Cookies / Auth
No / No
Kotlin Line Count
23
How curl to Kotlin conversion works
OkHttp is the most popular HTTP client for Kotlin and Android. This converter maps curl flags to OkHttp builder calls:-H becomes.addHeader(),-d becomes.toRequestBody(), and-u becomes a Basic auth header.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Converting an API's curl example into OkHttp for an Android or a JVM service.
- Rewriting a request so it fits a coroutine rather than a blocking call.
- Getting the request-builder chain right for a method with a body.
- Producing a snippet for a Kotlin coroutine wrapper.
- Checking how auth headers are attached before adding an interceptor.
Frequently Asked Questions
- Why OkHttp rather than the JDK client?
- OkHttp is the de-facto standard on Android, where java.net.http.HttpClient is unavailable below API 34. It also brings connection pooling, transparent GZIP and response caching by default, and Retrofit is built on top of it.
- How do I build a request body?
- In modern OkHttp use the extension: json.toRequestBody("application/json".toMediaType()). The older RequestBody.create(mediaType, json) argument order was reversed and is deprecated, which is a common source of confusing errors when following old examples.
- Must I close the response?
- Yes. The response body is a one-shot stream backed by a pooled connection — wrap the call in use { } or call close(), or you leak connections. Reading body?.string() consumes and closes it, but only if you actually read it.
- How do I set timeouts?
- On the client builder, not the request: OkHttpClient.Builder().connectTimeout(...).readTimeout(...).writeTimeout(...). Defaults are 10 seconds each. Build one client and share it — each instance carries its own pool and dispatcher threads.
- Can I call this from a coroutine?
- Use await() from the kotlinx-coroutines-adapter, or Retrofit's native suspend function support. Calling execute() directly on the main thread throws NetworkOnMainThreadException on Android.
- What are OkHttp interceptors used for?
- Cross-cutting concerns — attaching an auth header, logging, retrying, or adding a cache policy — applied to every request through one client. An application interceptor runs once; a network interceptor runs per redirect and retry.
- Which dispatcher should the call run on?
- `Dispatchers.IO`, since a blocking HTTP call would otherwise occupy a thread from the default compute pool. OkHttp's `enqueue` is already async, and the `await` extensions bridge it into a coroutine without blocking at all.
Common errors and gotchas
- Not closing the response body, which leaks connections — OkHttp is explicit about this.
- Creating a new OkHttpClient per request rather than sharing one, which discards the connection pool.
- Calling a synchronous execute on the main thread in Android, which throws.
- Forgetting the media type on a request body, which OkHttp requires rather than infers.
- Assuming a non-2xx throws, when OkHttp returns a response whose `isSuccessful` you must check.
Related Developer Utilities tools
RegExp Tester
Test regular expressions and inspect matches locally.
Regex Visualizer
Visual regex pattern diagram with live match highlighting and capture group annotations.
Subnet Calculator
Compute CIDR subnets, usable hosts, and network ranges.
Cron Parser
Translate cron syntax into plain English.
URL Parser
Break a URL into protocol, host, path, and query parts.
HTML Previewer
Paste HTML and see it rendered live in a safe, sandboxed preview.
HTTP Status Code Reference
Search and look up every HTTP status code and its meaning.
MIME Type Lookup
Find the MIME type for a file extension, or the extensions for a MIME type.