I have tried many methods of posting data to server but none of them worked and only response am getting is:
{\"Message\":\"An error has occurred.\"}
BASE_URL = PASS_YOUR_URL _params = parameters // AS NSMutableDictionary Try this.
NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
[params setValue:YOUR_VALUE forKey:@"cust_id"]; // here pass your field that you want to pass as parameter.
[params setValue:YOUR_VALUE forKey:@"Prod_ID"];
NSString *url = [BASE_URL stringByAppendingString:_action];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[manager POST:url parameters:_params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"response = %@", responseObject);
if( _success )
{
_success( responseObject ) ;
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@", error);
if( _failure )
{
_failure( error) ;
}
}];
Thank You @Tj3N, your answer was helpful.
Just needed to change encoding type. From NSASCIIStringEncoding to NSUTF8StringEncoding. Below is the Updated code.
NSString *post =[NSString stringWithFormat:@"customerid=%@&productid=%@&vendorid=%@&quantity=%@",cid,PID,VID,QQ];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSURL *url = [NSURL URLWithString:@"URL"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *urlData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"str:%@",str);
You are passing the data in query string where as the data which you want to post should be passed as NSDictionary object converted to Data.