0

i'm hiting an API in postman there i get result fine, this how i'm making POST request in postman,enter image description here

But when i hit same API in my application using objective c, i got errors, i'm passing parameters fine but result is not coming true, i'm confuse that why it is not showing results true, This is my code for POST request,

- (void)sendRequest

{

NSArray *userArray = [NSArray arrayWithObjects: @"ishaqshafiq@hotmail.com",nil];



NSDictionary *emp = @{@"lstUsers": userArray,
                      @"message":@"Your Order is Booked",
                      @"data": @{
                      @"type":@"text",
                      }};

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSString *urlLinkA=@"http://sajjenweb.azurewebsites.net/api/HubConnection/PostMobileNotification";


NSURL * url = [NSURL URLWithString:urlLinkA];

NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];


NSString *parameters = [NSString stringWithFormat:@"%@",emp];


NSLog(@"parameter %@",parameters);
[urlRequest setHTTPMethod:@"POST"];

//[urlRequest setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                       NSLog(@"Response:%@ ", response);
                                                       NSLog(@"Error is %@",error);

                                                       NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

                                                       //  NSLog(@"DDD %@",dictionary);




                                                       NSString *res = [dictionary valueForKey:@"recipients"];
                                                       NSLog(@"RR: %@", res);


                                                       NSString *msg=@"Successfully Submitted";

                                                       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Success"
                                                                                                                      message:msg
                                                                                                               preferredStyle:UIAlertControllerStyleAlert];


                                                       int duration = 2; // duration in seconds

                                                       dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                                                           [alert dismissViewControllerAnimated:YES completion:nil];
                                                       });

                                                   }];


NSLog(@"network error :");
[dataTask resume];

}

Hamza
  • 125
  • 1
  • 10
  • 2
    NSDictionary is not equal to JSON. Use [NSJSONSerialization](https://developer.apple.com/documentation/foundation/nsjsonserialization?language=occ) to get a json from it. – Desdenova Sep 26 '18 at 06:41
  • i didn't get can you explain or identify a bit? @Desdenova – Hamza Sep 26 '18 at 06:47
  • 1
    Print your dictionary and compare it with json. You'll see the difference. Then check this link to see how to use json serialization. https://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization/15813799 – Desdenova Sep 26 '18 at 06:57
  • My parameters are going in this way, parameter { data = { type = text; }; lstUsers = ( "ishaqshafiq@hotmail.com" ); message = "Your Order is Booked"; }. @Desdenova – Hamza Sep 26 '18 at 07:02
  • can u please identify that in my code? @Desdenova – Hamza Sep 26 '18 at 07:18
  • `NSString *parameters = [NSString stringWithFormat:@"%@",emp];` That's not the same. As said, use `NSJSONSerialization` (`dataWithJSONObject:options:error:`) to transform `emp` to `NSData`, then set `[urlRequest setHTTPBody:thatDataGenerated]`. For debug purpose if needed, use `NSString *paramsSentAsString = [[NSString alloc] initWithData: theDataGenerated encoding: NSUTF8String]; NSLog("DebugJSON: %@", paramsSentAsString);` – Larme Sep 26 '18 at 07:42
  • can u please correct it in my code , i'm not getting it like this. @Larme – Hamza Sep 26 '18 at 08:51
  • 1
    https://stackoverflow.com/questions/6368867/generate-json-string-from-nsdictionary-in-ios – Larme Sep 26 '18 at 10:14
  • Got it big thanks. @Larme – Hamza Sep 26 '18 at 11:27

0 Answers0