Link to home
Start Free TrialLog in
Avatar of graga
graga

asked on

iOS AFNetworking POST parameters sent from XCode are empty in URL.

Hi Experts,

I am very new into iOS programming and I need your help.

I have a simple code in XCode that uses GET to send data to URL and I want to change this to a POST.

When I test the call, both methods work okay, but running from XCode on simulator, GET works okay but POST returns empty value.

AFHTTPRequestOperationManager returns success.

Here is the code for both:

- (IBAction)cmdTestGET:(id)sender {

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
   
    [manager GET:@"http://mytestwebsite/index_test.php"
      parameters: @{@"method":  @"Test_GET",
                    @"data":    @"Hello GET!"}
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", [responseObject description]);
         }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", [error description]);
         }];
}

- (IBAction)cmdTestPost:(id)sender {
   
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
   
    [manager POST:@"http://mytestwebsite/index_test.php"
                parameters: @{@"method":  @"Test_POST",
                              @"data":    @"Hello POST!"}
                   success:^(AFHTTPRequestOperation *operation, id responseObject) {
                       NSLog(@"JSON: %@", [responseObject description]);                   }
                   failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                       NSLog(@"Error: %@", [error description]);
                   }];  
}


And they returned respectively:

JSON: {
    code = 1;
    data = OK;
    method = "AM_Test_GET";
    status = 200;
    value = "Hello GET!";
}

JSON: {
    code = 0;
    data = "<null>";
    method = "<null>";
    status = 200;
}


the php page that is called is attached
Avatar of Hamidreza Vakilian
Hamidreza Vakilian

I couldn't see your attachment file, please attach it again. I think the problem is from your server side and your above code seems fine.  However, if you are learning iOS, I strongly suggest you to work with NSURLSession instead of AFNetworking unless you have a reason. NSURLSession supports block based callbacks, easy to use and less overhead.
ASKER CERTIFIED SOLUTION
Avatar of graga
graga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of graga

ASKER

I have solved the problem as specified in my previous comment