Convert curl to Swift

Convert curl commands to Swift code using URLSession. curl2code runs entirely in your browser via WebAssembly — your data stays private. For Kotlin (Android), try our curl to Kotlin converter. For Objective-C, see curl to Objective-C. Below are ready-to-use Swift examples.

How to copy curl from your browser

  1. 1

    Open DevTools

    Press F12 or Ctrl+Shift+I to open your browser developer tools.

  2. 2

    Go to the Network tab

    Click the Network tab and perform the action that triggers the HTTP request.

  3. 3

    Copy as cURL

    Right-click the request → CopyCopy as cURL. Then paste it above.

Frequently Asked Questions

What is Swift URLSession?

URLSession is Apple's built-in HTTP client for iOS, macOS, watchOS, and tvOS. It handles data tasks, downloads, uploads, WebSocket, and background transfers. No package installation needed — it's part of Foundation. Supports HTTP/2, caching, and cookie management. curl2code generates URLSession code for all Swift conversions.

URLSession vs Alamofire — which should I use?

URLSession is built-in and covers most needs — use it to avoid third-party dependencies. Alamofire adds convenience: chainable request builders, automatic retry, response validation, and multipart uploads. For small projects, URLSession is sufficient. For similar built-in approaches, see our curl to Kotlin converter or curl to Objective-C.

How to handle authentication in Swift?

Set the header on URLRequest: request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization"). For Basic auth, encode with Data("\(user):\(pass)".utf8).base64EncodedString(). curl2code converts -u and Bearer header flags to Swift code automatically.

How to send multipart form data in Swift?

Construct the multipart body manually: generate a boundary string, append each field/file with CRLF separators, and set Content-Type: multipart/form-data; boundary=.... For easier multipart, use Alamofire's upload(multipartFormData:). curl2code handles -F flag conversion for URLSession.

How to handle HTTP errors in Swift?

URLSession returns an optional Error in completions — check for nil. Cast the response to HTTPURLResponse and check statusCode. With async/await (iOS 15+), use try await URLSession.shared.data(for: request) in a do/catch block for clean error handling.

How to use async/await with URLSession?

Swift 5.5+ supports async/await natively: let (data, response) = try await URLSession.shared.data(for: request). This replaces completion handler closures with linear code. Use Task { } to call from synchronous contexts. For concurrent requests, use async let or TaskGroup. For similar patterns, see curl to Dart.

How to set timeouts in Swift URLSession?

Configure on URLSessionConfiguration: config.timeoutIntervalForRequest = 30 (per-request) and config.timeoutIntervalForResource = 300 (total). Or set request.timeoutInterval = 30 on individual requests. These map to curl's --max-time and --connect-timeout options.

How to use a proxy with URLSession?

Set proxy on URLSessionConfiguration: config.connectionProxyDictionary = [kCFProxyHostNameKey: "proxy", kCFProxyPortNumberKey: 8080]. For SOCKS proxies, use kCFStreamPropertySOCKSProxy keys. URLSession also respects system-wide proxy settings configured in macOS/iOS settings.

How to send a POST request with JSON body in Swift?

Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d "{"key":"value"}" URL to Swift using curl2code. The generated code uses request.httpBody = try JSONEncoder().encode(data). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the URLSession code.

How to add Bearer token authorization in Swift?

Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates URLSession code with the auth header: request.setValue("Bearer YOUR_TOKEN", forHTTPHeaderField: "Authorization"). curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.

How to set Content-Type header in Swift?

curl2code converts curl -H "Content-Type: application/json" URL to URLSession code with the proper header: request.setValue("application/json", forHTTPHeaderField: "Content-Type"). For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct URLSession API.

Useful Links

curl Guides

Convert curl to Other Languages