I am trying to send some key-value to one of my page (php page) using AFNetworking. I am using AFHTTPRequestOperationManager for this operation, the post is happening successfully however I am receiving blank values on server side.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"name":@"1234", @"email":@"1234",@"regId":@"rty2345",@"device_flag":@"2"};
NSLog(@"%@", params);
manager.responseSerializer = [AFJSONResponseSerializer serializer]; // if response JSON format
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:@"http://xyz.in/app/apns/register.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
[self databaseInsertion];
ShowViewController *mapp = [[self storyboard]instantiateViewControllerWithIdentifier:@"showview"];
[self.navigationController pushViewController:mapp animated:YES];
NSLog(@"%@", responseObject);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}];
I am sending back a JSON string so that success block will be performed. But when I try to fetch the value at the server side, all values shows blank.
Then I tried to send data using NSMutableURLRequest *request which is also making same result, when I try to use the browser to send data like http://www.xyz.in/app/apns/register.php?name=15&email=1234®Id=r5678d&device_flag=2, it works well.
My Code using NSMutableURLRequest is below
NSString *post =[[NSString alloc] initWithFormat:@"name=%d&email=%d®Id=%@&device_flag=%d", status,[_emailTextField.text intValue],@"r5678d",2];
NSLog(@"%@",post);
NSURL *url=[NSURL URLWithString:@"http://xyz.in/app/apns/register.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];