2

With the Reference to these Posts Objective-C equivalent of curl request ,Executing curl based actions in Objective-C i tried Below Code for my Example I have the example data like below

curl --get  'https://www.example.com/xyz' \
     --user 'API_KEY:API_SECRET' \
     --data 'app_id=APP_ID' \
     --data 'metrics=users' \
     --data 'dimensions=day,new_device' \
     --data-urlencode 'conditions={"day":["between","2013-04-01","2013-04-07"]}'

How can I retrieve the data if I have the api key(production key),? and also how can I retrieve the data of users by using the above parameters? I tried doing it something like this , to pass the parameter to retrieve the data through the url .

-(void)doit
{
    NSString *userName =@"abc@v.com";
    NSString *password =@"cvbnm";

    NSError *myError = nil;


    NSMutableString *loginString = (NSMutableString*)[@"" stringByAppendingFormat:@"%@:%@", userName, password];
    NSLog(@"loginstring=%@",loginString);


    NSString *accept = [NSString stringWithFormat:@"application/example"];

    NSLog(@"accept %@",accept);
    NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@", loginString];
    NSLog(@"auth header =%@",authHeader);

    NSURL *url = [NSURL URLWithString:@"https://example.bnm/query"];

    NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
    request = [NSMutableURLRequest requestWithURL:url];

    [request setURL:url];
    [request setHTTPMethod:@"GET"];
[request setValue:@"users" forKey:@"metrics"];
[request setValue:@"value of api key" forKey:@"API_KEY"];
[request setValue:@"day" forKey:@"dimension"];
[request setValue:@"{"day":["between","2013-04-01","2013-04-07"]}" forKey:@"conditions"];

 NSError *error;
    NSURLResponse *response;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSLog(@"data : %@",data);

    NSString *result = [NSString stringWithCString:[data bytes] length:[data length]];
     NSLog(@"result = %@",result);
}

But something went wrong and 'm not getting the proper response. Please do help me with this , please let me know how to fix it , any help will be really great , please let me know if you need more information

Community
  • 1
  • 1
Ranjana
  • 43
  • 1
  • 1
  • 10

1 Answers1

0

The problem is with your lines that say [request setValue: forKey:];

Those don't actually set the request body, what you need to do is something like this:

NSString *stringData = @"app_id=APP_ID&metrics=users&foo=bar";
NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBodyData;

Then try posting that and see if it works for you!

Edit:

I also noticed you're not using the authentication, you're just logging authHeader

Mostafa Berg
  • 3,211
  • 22
  • 36
  • thank you so much for replying. As you said I have replaced it with what u said.but 'm getting the data as null and result nothing. – Ranjana Apr 07 '14 at 18:00
  • As you said I have edited, but 'm getting the response something like this 2014-04-07 23:25:51.800 FirstDemo[56329:70b] loginstring=abc@v.com:vmbnm 2014-04-07 23:25:51.800 FirstDemo[56329:70b] accept application/accept 2014-04-07 23:25:51.801 FirstDemo[56329:70b] auth header =Basic abc@v.com:vmbnm 2014-04-07 23:25:51.801 FirstDemo[56329:70b] after request 2014-04-07 23:26:00.449 FirstDemo[56329:70b] data : (null) 2014-04-07 23:26:00.449 FirstDemo[56329:70b] result = – Ranjana Apr 07 '14 at 18:04
  • if data is Nil then you should take a look at the content of the NSError, just put a breakpoint after the response is received or `NSLog(@"Error: %@", error);`, let me know what it says. – Mostafa Berg Apr 07 '14 at 18:08
  • Yeah , the error is something like this : rror: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x8d6ecc0 {NSErrorFailingURLStringKey=https://example.bnm/query, NSErrorFailingURLKey=https://example.bnm/query, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x8b9f280 "The network connection was lost."} – Ranjana Apr 07 '14 at 18:13
  • You're not by any chance using `example.bnm/query` as your URL right ? – Mostafa Berg Apr 07 '14 at 18:23
  • i am already using their sdk for one of my main application" ,this app is to track the userdata ,analytics .we have to make another app for admin tracking – Ranjana Apr 07 '14 at 18:44
  • again, why don't you use their SDK on this app ? it seems that basic requests are confusing you so why go the hard way ? the SDK will handle all the connections, you know ? – Mostafa Berg Apr 07 '14 at 18:47
  • "No sdk is for (creating) analytics not for records ,for recording/tracking the user data , we have to visit localystics website user portal or we hAve to use these api's" to retrieve the user data – Ranjana Apr 07 '14 at 18:53
  • Is there any other way to retrieve the data ? – Ranjana Apr 08 '14 at 06:16