4

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.

acecapades
  • 893
  • 12
  • 23
  • I see `-u` in your curl command and `username` and `password` in your mutable request. Do you know that your server can accept `username` and `password` parameters like this? – Michael Dautermann Jan 24 '12 at 06:18
  • What's the error? You don't know how to make a request? Or it doesn't respond? (btw you want to set the header fields' keys WITHOUT trailing colon.) –  Jan 24 '12 at 06:20
  • @MichaelDautermann: That's the thing, I'm not sure how my server accepts the arguments for username and password. (BTW, I'm using the API for imonggo POS). Would you know how I'll figure that out? Thanks.. – acecapades Jan 24 '12 at 06:27
  • @H2CO3: It responds but I get an error: rror Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x6ab39d0 {NSErrorFailingURLKey=https://acebonita.c2.imonggo.com/api/products.json, NSErrorFailingURLStringKey=https://acebonita.c2.imonggo.com/api/products.json, NSUnderlyingError=0x6ab2f40 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"} and HTTP Basic: Access denied. I'm guessing I have a wrong format for my header fields. btw thanks for the advise on removing the colons. – acecapades Jan 24 '12 at 06:30

3 Answers3

3

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:password and 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.

    // snip
    NSMutableUrlRequest* theRequest = [NSMutableUrlRequest ...]
    [MyClass httpAuthorizeRequest:theRequest withUsername:@"someuser" andPassword:@"mysecret"];
    // snip

+ (void)httpAuthorizeRequest:(NSMutableURLRequest*)request withUsername:(NSString*)username andPassword:(NSString*)password
{
    NSString* authorizationToken = [[NSString stringWithFormat:@"%@:%@", username, password] base64Representation];
    [request setValue:[NSString stringWithFormat:@"Basic %@", authorizationToken] forHTTPHeaderField:@"Authorization"];
}
Community
  • 1
  • 1
Sveinung Kval Bakken
  • 3,715
  • 1
  • 24
  • 30
0

I would suggest to use ASIHTTPRequest for converting cUrl to Objective C code. As I have used it and it is very easy to implement, I used the authentication of NSMutableUrlRequest but it didn't worked for me. So I am posting this for someone looking answer like me.

ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"https://acebonita.c2.imonggo.com/api/products.json"]];

[request setUsername:@"0efd245bfbf6bba4106c78146a23becba9105d1e"];
[request setPassword:@"x"];

[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request addRequestHeader:@"Accept" value:@"application/json"];
[request startSynchronous]; 

if([request error])
{
    NSLog(@"Code : %d, Error: %@",[[request error] code],[[request error] description]);
}
NSLog(@"Code: %d, Description: %@, Message: %@",[request responseStatusCode],[request responseData],[request responseStatusMessage]); 
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
0

You could just build cURL for iOS. Here is a Makefile to build an iOS lib.

Save that file to your home folder as 'build-cURL.sh' then open Terminal and paste:

export CURL_VERSION=7.23.0 
sh ~/build-cURL.sh

Then the script should build a static library that you can add to your project. Then you will want to add the .h files and it will be ready to go.

I used lipo to create a fat-binary of the library so it will work in both the sim and the device, download it from here. Then add the curl folder to your project, and add the library. To do this click on your project in the upper left, click on your target, hit the "Build Phases" tab, drop down "Link Binary with Libraries", hit the plus icon and choose the .a file.

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58
  • Good idea but I don't know how to build cURL for iOS. Can you help me out? Thanks a lot! – acecapades Jan 24 '12 at 06:33
  • Just save the file to your home folder as 'build-cURL.sh' then open Terminal and paste: sh ~/build-cURL.sh It will then download the cURL source code and build a static library you can add to your project. Then you will want to add the .h files and it will be ready to go. – Jeshua Lacock Jan 24 '12 at 06:41
  • Ok I followed the instructions. And here's what I got from the terminal "tar: Unrecognized archive format tar: Error exit delayed from previous errors." and I got a curl-.tar.gz What will I do after that? Thanks a lot! – acecapades Jan 24 '12 at 06:54
  • Sorry - missed a step. Before you run that command in the terminal paste this: export CURL_VERSION=7.23.0 – Jeshua Lacock Jan 24 '12 at 07:00
  • Oh there! I got a file "curl-7.23.0.tar.gz". Then what will I do with it? Thanks again :) – acecapades Jan 24 '12 at 07:13
  • p.s. you can download the precompiled libs from: http://code.google.com/p/ios-static-libraries/downloads/list – Jeshua Lacock Jan 24 '12 at 07:15