8

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:

// 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.

Brian
  • 14,610
  • 7
  • 35
  • 43
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
  • Please change your request to "POST" method [request setHTTPMethod:@"POST"]; – Kapil Kumar Jul 12 '12 at 12:11
  • Also you have created the wrong parameter request NSString *loginString = [NSString stringWithFormat:@"%@:%@", API_USERNAME, API_PASSWORD]; it should be like this NSString *loginString = [NSString stringWithFormat:@"username=%@&password=%@", API_USERNAME, API_PASSWORD]; – Kapil Kumar Jul 12 '12 at 12:15
  • @KapilGhai I've tried both your suggestions but same error unfortunately. – Lloyd Powell Jul 12 '12 at 12:19
  • try this NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://username:password@example.com"]]; [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL]; and also removed the HTTPBody Mthod – Kapil Kumar Jul 12 '12 at 12:32
  • @KapilGhai - funnily enough, I tried that just before your comment and it worked successfully. You know what they say. You wait ages for one and then they all come together! Stick it in as an answer and I'll award it to you. – Lloyd Powell Jul 12 '12 at 12:50
  • I don't know what the problem was but I changed the url to http://username:password@www.example.com... and it worked. – Lloyd Powell Jul 12 '12 at 12:55
  • How should we write this curl syntax to Objectivec curl -v api.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9‌​183IvxFyZp" \ -d "grant_type=client_credentials" – Bhoomi Jagani Sep 16 '14 at 08:52

2 Answers2

11

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];
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
0

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.

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61