I’m having a difficulty transforming this curl command for my Objective C Code:
curl -u 0ef106c78146a23becba9105d1e:X -H "Accept: application/json" -H "Content-Type: application/json" https://boomboom.c27.imonggo.com/api/products.json
Here’s my objective C code:
NSError *theError = NULL;
NSURL *theURL = [NSURL URLWithString:@"https://boomboom.c2.imonggo.com/api/products.json"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setValue:@"0ef106c78146a23becba9105d1e" forHTTPHeaderField:@"username"];
[theRequest setValue:@"x" forHTTPHeaderField:@"password"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Accept:"];
[theRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type:"];
NSURLResponse *theResponse = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
Most likely my problem is with the header fields.
First of all, you don’t need the colon in the HTTP header field name.
Second, you’re doing the HTTP authentication wrong. You need to concat your username and password,
username:passwordand Base64 encode the concatenated string data.Use the base64 value as value for the header field
Authorization, use the code below and drop in a Base 64 encoding implementation.