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

graga
graga used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hamidreza VakilianSenior iOS/Android Developer

Commented:
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.
Commented:
Hi,

I have found the problem:

manager.requestSerializer = [AFJSONRequestSerializer serializer];

removing this line fixed it.

Author

Commented:
I have solved the problem as specified in my previous comment

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial