1
我想在我的iOS應用程序中執行POST請求。在iOS中發佈請求?
以下行按預期工作在我的Mac
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"email":"[email protected]","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\":\"%@\"}",@"[email protected]",@"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];
我缺少/做錯了嗎?