Send multipart form-data with cURL in Kotlin
Paste a curl command that sends multipart form-data and get idiomatic Kotlin code. We pick the right serialization for OkHttp and set the correct Content-Type.
Quick answer
To convert a cURL command to Kotlin, paste it into the Toolpaper workbench below. It parses the command in your browser and emits idiomatic Kotlin code — headers, query string, request body, authentication (Bearer, Basic, API key), and cookies are all wired up automatically. No data is uploaded; conversion is fully client-side.
Prefer a different target? See the full cURL to Kotlin reference, or jump to the same guide in JavaScript, Node.js, or Python.
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File
import okhttp3.RequestBody.Companion.asRequestBody
val client = OkHttpClient.Builder().followRedirects(false).followSslRedirects(false).build()
val body = MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("file", "/path/to/photo.png", File("/path/to/photo.png").asRequestBody("application/octet-stream".toMediaTypeOrNull()))
.addFormDataPart("caption", "Hello")
.build()
val request = Request.Builder()
.url("https://api.example.com/upload")
.method("POST", body)
.build()
client.newCall(request).execute().use { response ->
println(response.body?.string())
}Example
A representative curl command for this scenario. Paste your own above to convert an exact match.
curl -X POST https://api.example.com/upload \
-F "file=@/path/to/photo.png" \
-F "caption=Hello"Implementation notes
- Content-Type is detected from -H headers first, then inferred from the body shape when the header is missing.
- Each -F flag becomes a distinct part; file@ paths are surfaced as OkHttp file handles so the multipart boundary stays valid.
- Charset directives on Content-Type (e.g. application/json; charset=utf-8) survive the round trip.
Common mistakes
- Setting Content-Type manually and then also using OkHttp's helper — you end up with duplicate headers or the wrong one wins.
- Forgetting @ in -F "file=@upload.png" — without it curl sends the literal string, not the file. The generator flags this.
- Trusting the browser DevTools "Copy as cURL" to preserve charset — it often drops it. Clean panel restores common defaults.
Common questions
- How is multipart form-data encoded in the generated Kotlin?
- -F flags translate to multipart form-data with proper boundary handling — files={...} in Python, MultipartFormDataContent in C#, MultipartBody.Part in Java/Kotlin.
- Does it preserve Content-Type?
- Yes. The Content-Type header is honored and matched against the body so serialization stays consistent.
- Is anything uploaded?
- No. Parsing and generation happen entirely in your browser.
More payload types in Kotlin
Kotlin hub →The same guide in other languages
All guides →In JavaScript
/how-to/send-multipart-with-curl-in-javascript
In Node.js
/how-to/send-multipart-with-curl-in-node
In Python
/how-to/send-multipart-with-curl-in-python
In Go
/how-to/send-multipart-with-curl-in-go
In PHP
/how-to/send-multipart-with-curl-in-php
In Ruby
/how-to/send-multipart-with-curl-in-ruby
In Rust
/how-to/send-multipart-with-curl-in-rust
In Java
/how-to/send-multipart-with-curl-in-java
Full Kotlin reference on the cURL to Kotlin hub, or open the main converter for all 18 targets.