Birendra
Birendra

Reputation: 625

How to send json data in the Http request using AFNetworking

I have to pass data like json format to the server

{"email":"[email protected]","password":"abc"}

and i have used this code but data is not pass to the server please help me..

    NSDictionary *dict1=@{@"email": @"[email protected]"};
    NSDictionary *dict2=@{@"password": @"biren"};
    services *srv=[[services alloc]init];

    NSString *str=@"http://emailsending.in/setting_saver_api/";
    NSString *method=@"login.php";
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

    [dict setValue:dict1 forKey:@"email"];

    [dict setValue:dict2 forKey:@"password"];


    [srv postToURL:str withMethod:method andParams:dict completion:^(BOOL success, NSDictionary *responseObj)

     {
         NSLog(@"res :%@",responseObj);
         NSLog(@"%d",success);

         NSLog(@"Successfully..................");


     }];

Upvotes: 0

Views: 668

Answers (3)

iosDev_1205
iosDev_1205

Reputation: 798

you can check your link at https://www.hurl.it/

1)give your http link

2)select your method type "Get" or "Post"

3)add the parameters

and check the response.If you get the same response contact your backend team.

Upvotes: 1

iosDev_1205
iosDev_1205

Reputation: 798

Try this,if this helps

  NSString *str = @"http://emailsending.in/setting_saver_api/"; 

NSString*method = @"login.php";

    services *srv=[[services alloc]init];

    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary]; [dict1 setValue:@"[email protected]" forKey:@"email"];

    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary]; [dict2 setValue:@"biren" forKey:@"password"];


    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

        [dict setValue:dict1 forKey:@"email"];

        [dict setValue:dict2 forKey:@"password"];

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:str parameters:dict success:^(NSURLSessionDataTask
    *task, id responseObject) {

            NSLog(@"JSON: %@", responseObject);
            //here is place for code executed in success case

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

            //here is place for code executed in error case
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                                message:@"Sorry, try again."
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView show];

            NSLog(@"Error: %@", [error localizedDescription]); }];

Upvotes: 0

adarshasp
adarshasp

Reputation: 50

Try this. 1) Convert your dictionary into json using NSJSONSerialization and store it in NSdata.

NSData * incidentData = [NSJSONSerialization dataWithJSONObject: params options:0 error:&err];

2) Post the NSdata containing json to your server

Upvotes: 0

Related Questions