Convert curl to Objective-C
Convert curl commands to Objective-C code using NSURLSession. curl2code runs entirely in your browser via WebAssembly — your data stays private. For Swift, try our curl to Swift converter. For C, see curl to C. Below are ready-to-use Objective-C examples.
How to copy curl from your browser
- 1
Open DevTools
Press F12 or Ctrl+Shift+I to open your browser developer tools.
- 2
Go to the Network tab
Click the Network tab and perform the action that triggers the HTTP request.
- 3
Copy as cURL
Right-click the request → Copy → Copy as cURL. Then paste it above.
Frequently Asked Questions
What is NSURLSession?
NSURLSession is Apple's Foundation framework API for HTTP networking in Objective-C (and Swift). It handles data tasks, downloads, uploads, and background transfers. Available on iOS 7+, macOS 10.9+. No third-party dependency needed — it's the standard way to make HTTP requests in Apple platforms. curl2code generates NSURLSession code for Objective-C.
NSURLSession vs AFNetworking — which to use?
NSURLSession is the built-in solution — sufficient for most needs and zero dependencies. AFNetworking was historically popular but is now in maintenance mode; its successor is Alamofire (Swift). For new Objective-C projects, NSURLSession is recommended. For Swift, see our curl to Swift converter.
How to handle authentication in Objective-C?
Set the header on NSMutableURLRequest: [request setValue:@"Bearer token" forHTTPHeaderField:@"Authorization"]. For Basic auth, encode credentials with NSData and base64EncodedStringWithOptions:. curl2code converts -u and Bearer flags to the correct Objective-C syntax.
How to send multipart form data in Objective-C?
Construct the multipart body with NSMutableData: append boundary strings, field data, and file data with proper CRLF separators. Set Content-Type with the boundary. This is manual but gives full control. AFNetworking simplifies this with AFHTTPRequestSerializer.
How to handle HTTP errors in Objective-C?
NSURLSession completion handlers provide an NSError parameter — check it for network/transport errors. Cast the response to NSHTTPURLResponse and check statusCode. Use NSURLSession delegate methods for more granular error handling including authentication challenges and redirects.
How to work with completion blocks in NSURLSession?
NSURLSession uses block-based callbacks: [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { ... }]. Always call [task resume] to start. For sequential requests, nest blocks or use dispatch groups with dispatch_group_notify.
How to set timeouts in Objective-C?
Set on NSURLSessionConfiguration: config.timeoutIntervalForRequest = 30 and config.timeoutIntervalForResource = 300. Or per-request: request.timeoutInterval = 30. Timeout errors arrive as NSURLErrorTimedOut in the error parameter. These map to curl's --max-time option.
How to use a proxy with NSURLSession?
Set connectionProxyDictionary on NSURLSessionConfiguration with keys like (NSString *)kCFNetworkProxiesHTTPProxy and port. NSURLSession respects system-wide proxy settings by default on macOS and iOS. For PAC files, configure through system preferences.
How to send a POST request with JSON body in Objective-C?
Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d "{"key":"value"}" URL to Objective-C using curl2code. The generated code uses [request setHTTPBody:[NSJSONSerialization dataWithJSONObject:dict options:0 error:nil]]. curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the NSURLSession code.
How to add Bearer token authorization in Objective-C?
Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates NSURLSession code with the auth header: [request setValue:@"Bearer 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 Objective-C?
curl2code converts curl -H "Content-Type: application/json" URL to NSURLSession 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 NSURLSession API.