Converting a CURL Command to Objective-C

I find it difficult to convert this curl command to 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 is 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.

+4
source share
3 answers

First of all, you do not need a colon in the name of the HTTP header field.

Secondly, you are using HTTP authentication incorrectly. You need to specify your username and password, username:password and Base64 encode concatenated string data. Use the base64 value as the value for the Authorization header field, use the code below and discard the Base 64 encoding.

  // 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"]; } 
+3
source

You can just create cURL for iOS. Here is the Makefile for building the iOS lib.

Save this file in 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 create a static library that you can add to your project. Then you want to add the .h files and it will be ready to go.

I used lipo to create a live binary library file so that it works both on the sim and on the device, downloading 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 corner, click on your goal, click on the “Build Phases” tab, omit “Link Binary Files to Libraries”, click on the “plus” icon and select the .a file.

0
source

I would suggest using ASIHTTPRequest to convert cUrl code to Objective C. Since I used it and it is very easy to implement, I used NSMutableUrlRequest authentication, but that did not work for me. So I am posting this to someone who is looking for an 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]); 
0
source

Source: https://habr.com/ru/post/1392619/


All Articles