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
- Parses -X, -H, -d, --data-raw, --json, -u, -b, -A, -e and -G the way curl does
- Emits URLSession code that needs nothing beyond Foundation
- JSON bodies rendered as a Swift dictionary and encoded with JSONSerialization
- Basic auth rebuilt from the credentials, with base64EncodedString in the code
- Method inferred from the presence of a body, or taken from -X
- Warns about curl flags that have no meaningful Swift equivalent
- Copy the code or download it as a .swift file
How to use the cURL to Swift
- Paste the curl command, including its flags and body
- Read the generated Swift on the right
- Check the warnings for any flags that could not be translated
- 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
- The request works in curl but fails in the app. — Check App Transport Security. iOS blocks plain HTTP by default, so an http:// endpoint that curl reaches happily needs an exception in your Info.plist or an https URL.
- The generated code compiles but never prints anything. — URLSession is asynchronous. In a command-line tool the process exits before the completion handler runs — keep the run loop alive, or switch to the async/await form of data(for:).
- A -k or --insecure flag was ignored. — Deliberately. Skipping certificate validation needs a URLSessionDelegate and is a decision worth making explicitly rather than having generated for you.
- Special characters in the body come out wrong. — Check the quoting in the original command. A body inside double quotes has already been through your shell, so anything it expanded is baked into what curl received.
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
- cURL to Fetch — Convert a curl command to a JavaScript fetch() call.
- cURL to Python — Convert a curl command to Python requests code.
- cURL to Node.js (axios) — Convert a curl command to Node.js axios code.
- cURL to Java — Convert a curl command to Java 11 HttpClient code.
- cURL to Dart — Turn a curl command into Dart code for Flutter using package:http.
- API Request Client — Send HTTP requests, build headers and params, inspect responses — your last 25 requests are saved locally.
All ArrayKit tools