cURL converter · Swift
cURL to Swift
Paste a curl command and get a Swift snippet using URLSession's async data(for:) API. We hand-roll multipart bodies with Data, base64-encode -u into an Authorization header, and put the timeout on a URLSessionConfiguration.
InputcURL command
1 secret
MethodPOST·Headers2·Body46 B· Chars 187
OutputApple · URLSession(async/await)
import Foundation
let session = URLSession.shared
var request = URLRequest(url: URL(string: "https://api.example.com/v1/messages")!)
request.httpMethod = "POST"
request.setValue("Bearer sk_test_123", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = "{\"to\":\"+15551234567\",\"body\":\"Hello from curl\"}".data(using: .utf8)
let (data, response) = try await session.data(for: request)
print(String(data: data, encoding: .utf8) ?? "")
// Note: to disable redirects, implement URLSessionTaskDelegate.urlSession(_:task:willPerformHTTPRedirection:...) and return nil.Inspect
Top guides for Swift
All guides →Common questions
- URLSession or Alamofire?
- URLSession is the stdlib default and ships everywhere. Alamofire is nicer for multipart and retry, but it's an extra dependency — we emit URLSession so the snippet compiles as-is.
- Is the multipart body correct?
- Yes, but it's hand-assembled from Data because Foundation has no built-in multipart. For big files switch to URLSession.uploadTask(with:fromFile:) with a body written to a temp URL.
- async/await, or completion handlers?
- async/await. It compiles on iOS 15+ / macOS 12+ and matches what modern Swift codebases use. Drop-in completion-handler output is on the shortlist.
- Is my curl sent anywhere?
- No. Parsing and code generation happen entirely in your browser.
Also convert cURL to
All guides →Same parser, same idiomatic output — swap the target language whenever your stack changes. Every hub carries its own reference guides for methods, payloads, and auth.