Reputation: 1079
I create a dictionary with fields "email" and "password" and use these params as the body of my request. I get the response from my server that I am missing the parameters email and password - I am positive this is not an issue on the server side. This is how I'm forming the request:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *urlString = @"xxxxxx.xxxx.com/userServices/login";
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSDictionary *params = @{@"email" : email, @"password" : password};
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:nil];
[request setHTTPBody:bodyData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setTimeoutInterval:45];
[request setHTTPShouldHandleCookies:NO];
Any idea why server would not be recognizing my params?
Upvotes: 0
Views: 86
Reputation: 89
Try this it will work for you:
NSDictionary * requestDict =[NSDictionary dictionaryWithObjectsAndKeys:@"email",@"YourEmail",@"password",@"YourPassword", nil];
NSMutableURLRequest * request =[[ NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:BASE_URL]];
NSData * postData =[ NSJSONSerialization dataWithJSONObject:requestDict options:0 error:nil];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection
sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
NSDictionary * dictServerResponse =[ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"data %@",dictServerResponse);
// DO YOUR WORK HERE
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
Upvotes: 0
Reputation: 1412
NSString *post =[[NSString alloc] initWithFormat:@"email=%@&password=%@",[txtUserEmail text],[txtUserPassword text]];
// what ever in you case e.g [txtuseremail text] replaced by email
NSURL *url=[NSURL URLWithString:@"http://localhost/abc.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/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
try this may be it will help you
Upvotes: 0
Reputation: 539
when creating your dictionary, I would do this
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:[NSString stringWithFormat:@"%@", email] forKey:@"email"];
[params setValue:[NSString stringWithFormat:@"%@", password] forKey:@"password"];
after that I would do this
NSData *postData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
You might need to give your request a content length
[request setValue:[NSString stringWithFormat:@"%i",postData.length] forHTTPHeaderField:@"Content-Length"];
Upvotes: 1