我正在尝试在iOS应用程序中执行POST请求。
以下行在Mac的命令行中按预期工作
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"email":"frank.smith@gmail.com","password":"frankSmith"}' http://server.address
但是,我似乎无法将其正确转换为iOS。这是我尝试过的:
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];
我错过了什么/做错了什么?
答案 0 :(得分:1)
想出来:
我只需要添加这一行:
[request setHTTPShouldHandleCookies:NO];