cURL to Swift

Paste a curl command and get Swift code that makes the same request with URLSession.

cURL to Swift parses your command in the browser. Tokens and credentials in the command never leave your device.

Try the API client

About cURL to Swift

API documentation speaks curl and iOS apps speak URLSession, and translating between them by hand is repetitive work that invites small mistakes — a header dropped, a body left as a string when it should be JSON, an auth flag quietly ignored. This converter parses the curl command the way curl itself does, including the flags that change the method or fold credentials into a header, and emits Swift that uses only Foundation. A JSON body becomes a Swift dictionary passed through JSONSerialization rather than an opaque string, so it stays readable and easy to edit. Basic auth is rebuilt from the decoded credentials instead of being pasted in as a Base64 blob.

Features

How to use the cURL to Swift

  1. Paste the curl command, including its flags and body
  2. Read the generated Swift on the right
  3. Check the warnings for any flags that could not be translated
  4. Copy the code, or download it as a .swift file

Example

Input

curl https://api.example.com/users -H 'Content-Type: application/json' -d '{"name":"Ada"}'

Output

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

The method is inferred from the body, exactly as curl would infer it.

Common errors & troubleshooting

Frequently asked questions

Does the generated Swift work on iOS, macOS and Linux?
Yes. It uses URLSession from Foundation, which is available on Apple platforms and in swift-corelibs-foundation on Linux, so the same file compiles for a server-side project too.
Can I get async/await code instead of a completion handler?
The generated form uses a data task with a completion handler, which works everywhere. Swapping it for `let (data, response) = try await URLSession.shared.data(for: request)` is a one-line change on a modern deployment target.
How is a JSON body handled?
It is parsed and rendered as a Swift [String: Any] dictionary, then encoded with JSONSerialization. That keeps the payload readable and lets you edit values without touching an escaped string.
What happens to cookies from a -b flag?
They become a Cookie header on the request. Note that URLSession has its own cookie storage, which may add or override cookies unless you configure the session otherwise.
Is my curl command uploaded when I convert it to Swift?
No. The command is parsed and the code generated in your browser, which matters because curl commands from documentation and logs so often contain live API keys.

Related tools

All ArrayKit tools