curl to Swift
Convert curl commands to Swift URLSession and URLRequest code. Supports GET, POST, PUT, DELETE with headers, request body, and authentication.
Presets:
HTTP Method
POST
Headers Detected
2
Body Bytes
44 B
Swift Size
19 lines (634 B)
Looks like this contains credentials. They are converted in this tab only — not uploaded, and not included in the link that Share or Copy link produces.
About curl to Swift conversion
Swift's URLSession is the native HTTP client for iOS and macOS apps. This converter maps curl flags to Swift code:-H becomessetValue(_:forHTTPHeaderField:), and -d becomeshttpBody. The output uses URLSession.shared.dataTask with a completion handler — the standard pattern for Swift networking.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Converting an API's curl example into URLSession code for an app.
- Reproducing a captured request inside an existing Swift client.
- Working out where App Transport Security will object before the app is built.
- Producing a snippet for a playground or a small tool.
- Choosing between the async and completion-handler forms.
Frequently Asked Questions
- Why does my http:// URL fail on iOS?
- App Transport Security blocks cleartext HTTP by default. curl has no such restriction. You must use https, or add an NSAppTransportSecurity exception in Info.plist — which App Review scrutinises, so treat it as a last resort.
- How do I use async/await?
- let (data, response) = try await URLSession.shared.data(for: request). The older completion-handler dataTask(with:) form still works and is required below iOS 15, but you must remember to call .resume() — a task that is never resumed silently does nothing.
- How do I read the status code?
- Cast the URLResponse: guard let http = response as? HTTPURLResponse. URLSession does NOT throw on a 404 — only on transport errors — so without this check a failed request looks successful.
- How do I send a JSON body?
- request.httpBody = try JSONEncoder().encode(payload) and set request.setValue("application/json", forHTTPHeaderField: "Content-Type"). Also set request.httpMethod = "POST" — the default is GET, and a GET with a body is silently dropped.
- How do I set a timeout?
- request.timeoutInterval for a single request, or timeoutIntervalForRequest and timeoutIntervalForResource on a URLSessionConfiguration. The default request timeout is 60 seconds.
- What runs on the main actor?
- URLSession's async methods return on a background context, so any UI update afterwards must hop to the main actor — marking the method `@MainActor` or wrapping the update. Swift concurrency makes this a compile-time error rather than a runtime glitch.
- How are background transfers handled?
- With a background `URLSessionConfiguration`, which hands the transfer to the system so it continues after the app is suspended. It requires the delegate API rather than async/await, and the app is relaunched to receive the completion.
Common errors and gotchas
- Forgetting App Transport Security blocks plain HTTP by default, so a working curl fails in the app.
- Setting a body on a GET, which URLSession may drop silently.
- Assuming a non-2xx surfaces as an error, when you must cast the response and check the status.
- Using a shared session for requests that need different caching or cookie behaviour.
- Retaining a completion handler in a way that creates a cycle.
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.