Objective-C equivalent of curl request
I’m trying to manipulate this curl request in Objective-C:
curl -u username:password "http://www.example.com/myapi/getdata"
I’ve implemented the following and I’m getting a data get error
Domain=kCFErrorDomainCFNetwork Code=303 with
// Make a call to the API to pull out the categories NSURL *url = [NSURL URLWithString:@"http://www.example.com/myapi/getdata"]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; // Create the username:password string for the request NSString *loginString = [NSString stringWithFormat:@"%@:%@", API_USERNAME, API_PASSWORD]; // Create the authorisation string from the username password string NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; [request setURL:url]; [request setHTTPMethod:@"GET"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSError *error; NSURLResponse *response; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
I was hoping that someone could spot where I am going wrong with my attempt to manipulate the curl request and point me in the correct direction. Is there something obvious I’m missing out? The data returned from the API is in a JSON format.
- cURL with Alamofire - Swift - multipart/form-data
- Error installing Crashlytics - SSL peer handshake failed
- Executing curl based actions in Objective-C
- crashing on ios device while navigation using UIModalTransitionStylePartialCurl in xcode 4
- How to convert CURL command to Swift
- NSURLConnection Authorization Header not Working
2 Solutions Collect From Internet About “Objective-C equivalent of curl request”
I found that the best thing to do was to not attempt the authentication within the code and to put it straight in the URL itself. The working code looks as follows:
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://%@:%@@www.example.com/myapi/getdata", API_USERNAME, API_PASSWORD]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setURL:url]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSError *error; NSURLResponse *response; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
This guide seems to do what you’re looking for:
Just as an FYI, a lot of classes accept initWithData, and
NSData has a method
dataWithContentsOfURL, if you want to avoid setting up
NSURLConnections yourself this could be an easier way of achieving what you’re looking for.
- Disable autoplaying video for UIWebView
- How to read RFID using iPhone SDK
- Xcode – symbol(s) not found for architecture x86_64 (iOS Lib)
- Zbar SDK is not working in iOS6
- Cannot access AppDelegate in XCTestCase objects (or anything that references it)
- Does File Attribute contain millisecond? Objective -c
- IONIC framework Mobile Application Performance Issue
- How to prevent the keyboard from showing (not dismissing) on a TextView?
- How get UITableView IndexPath from UITableView iphone?
- iOS Dismiss and Present view controller custom animation
- GMSMapView myLocation not giving actual location
- Xcode 8.0 beta give an error after import Firebase
- How do I make an OS X application react when a file, picture, etc is dropped on its dock icon?
- Change color of cursor in UITextField
- Difference between “precondition” and “assert” in swift?