迅速なiOSでpostメソッドを使用してフォームデータでRest APIを呼び出す方法はありますか?POSTメソッドを使用してフォームデータでREST APIを呼び出す方法はありますか?
私はスウィフトのために同様のコードが「ポスト」メソッド使用して形成されたデータと残りのAPIを打つしたい:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSString *encodedUrl = [[NSString stringWithFormat:@"%@%@", self.BaseUrl, strMethodName]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *serverUrl = [NSURL URLWithString:encodedUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSError *error = nil;
NSMutableString *body = [NSMutableString string];
for (NSString *key in requestDict) {
NSString *val = [requestDict objectForKey:key];
if ([body length]){
[body appendString:@"&"];
}
[body appendFormat:@"%s=%s", [[key description] UTF8String], [[val description] UTF8String]];
}
NSMutableData *postData = (NSMutableData *)[body dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
// [request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
Alamofireがベストです。このうち
チェック –