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 NSErrorFailingURLKey=http://www.example.com/myapi/getdata:

  • Xcode 7.0.1 - Invalid Swift Support - cocoapods HOW TO FIX?
  • instantiateViewControllerWithIdentifier - Storyboard id set but still not working
  • iOS: Pre install SSL certificate in keychain - programmatically
  • The dependency `AFNetworking (~> 2.5)` is not used in any concrete target
  • Scroll editor in Xamarin Forms into view
  • MPMoviePlayer done button issue
  • // 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.

    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:
    http://deusty.blogspot.co.uk/2006/11/sending-http-get-and-post-from-cocoa.html

    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.