1

I'm trying to call API which passes JSON data to server. Below is my code

NSDictionary *dictParam = [[NSDictionary alloc] initWithObjectsAndKeys:@"ABC", @"username", @"abc@xyz.com", @"email", @"linkedin", @"social", nil];

if([NSJSONSerialization isValidJSONObject:dictParam]){
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictParam options:0 error:&error];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    request.HTTPMethod = @"POST";
    request.HTTPBody = jsonData;


    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if(error == nil){
            NSDictionary *dictResult = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSLog(@"Result after login : %@",dictResult);
            NSLog(@"String : %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        }
        else{
            NSLog(@"Error in Login : %@",error);
        }
    }];
    [task resume];
}

But my problem is, data are not being passed. At server side they don't get any data. I'm unable to get where what am I missing.

It's an http connection.

Any help will be appreciated. Thanks in advance

Ankita Shah
  • 2,178
  • 3
  • 25
  • 42

0 Answers0