0

There is an api that I need to post the geoPoint, then get data, which works fine on Postman. Like shown in the image, this api works fine on Postman

In my code, I use the following way to call api

NSArray *geoPoint = @[@114.33f, @22.44f];

NSDictionary *geoPointDic = @ {@"geoPoint" : geoPoint};
NSDictionary *inData = @{
                         @"action" : @"getNearbyEventList",
                         @"data" : geoPointDic};

NSDictionary *parameters = @{@"data" : inData};

NSLog(@"geoPoint is %@", geoPoint);
NSLog(@"upcoming events parameters %@", parameters);

[_manager POST:GetURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  responseObject) {

    NSLog(@"responseObject is %@", responseObject);
    NSLog(@"responseObject - data is %@", responseObject[@"data"]);

    NSArray *eventsArray = responseObject[@"data"];

But the output is

when NSLog parameters:

events parameters {
    data =     {
        action = getNearbyEventList;
        data =         {
            geoPoint =             (
                "114.33",
                "22.44"
            );
        };
    };
}

the api response looks like

I guess my parameters might be in wrong format, so I tried the following way, but no one works

//NSArray *geoPoint = [[NSArray alloc] init];

//NSArray *geoPoint = [[NSArray alloc] initWithObjects:@"114", @"22", nil];

//NSMutableArray *geoPoint = [[NSMutableArray alloc] init];

//[geoPoint addObject:@114];

//[geoPoint addObject:@22];

//NSArray *geoPoint = [[NSArray alloc] initWithObjects:@"114", @"22", nil];

//NSString *geoPoint = @"[114.33,22.44]";

So, could anyone tell me how to get the data like shown in the Postman, please?

Thanks a lot!

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81

1 Answers1

0

You can try:

 NSData *dataBody = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil];
 NSString *json = [[NSString alloc] initWithData:dataBody encoding:NSUTF8StringEncoding];
 NSLog(@"%@",json);

Object json same is data you send to server.

Son Pham
  • 1,516
  • 1
  • 14
  • 22
  • Thank you so much for your answer. I have tried. But there will be "Request failed: internal server error (500)", if I don't send NSDictionary but use NSString as parameters. > – Alice King Jun 21 '17 at 07:05
  • I think it useful for you. https://stackoverflow.com/questions/19099448/send-post-request-using-nsurlsession – Son Pham Jun 21 '17 at 07:17