I’m trying to do a POST request in my iOS application.
The following line works as intended in command line of my Mac
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"email":"frank.smith@gmail.com","password":"frankSmith"}' http://server.address
However, I can’t seem to convert it properly to iOS. Here is what I’ve tried:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",SERVER_ADDRESS]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *messageBody = [NSString stringWithFormat:@"{\"email\":\"%@\",\"password\":\"%@\"}",@"frank.smith@gmail.com",@"frankSmith"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
What am I missing/doing wrong?
Figured it out:
I just needed to add this one line:
[request setHTTPShouldHandleCookies:NO];