AFHTTPRequestOperation *requestOperation = [manager POST:urlString parameters:nil constructingBodyWithBlock:^(id
// 上傳圖片檔案
UIImage *uploadImage = [UIImage imageNamed:@"img_test.png"];
NSData *imageData = [CommonObject reduceImageSize:uploadImage];
[formData appendPartWithFileData:imageData name:@"file" fileName:uploadFileName mimeType:@"text/plain"];
// 也可以包含其他request parameter
[formData appendPartWithFormData:[@"Karen_Chang" dataUsingEncoding:NSUTF8StringEncoding]
name:@"UserId"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation , NSError *error) {
NSLog(@"%@", error);
}];
若request parameter是NSArray,NSArray又由一堆JSON Object組成,該如何append formData???
直接將NSArray轉NSData
NSData *inputArray1 = [NSKeyedArchiver archivedDataWithRootObject:origionalArray];
[formData appendPartWithFormData:inputArray1 name:@"ArrayInput"];
會拋出如下錯誤
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo={NSUnderlyingError=0x7ff24545fa50 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"
後來改寫法如下,就成功了^^
NSError *writeError = nil;
NSData *inputArray2 =
[NSJSONSerialization dataWithJSONObject:origionalArray
options:NSJSONWritingPrettyPrinted
error:&writeError];
[formData appendPartWithFormData:inputArray2 name:@"ArrayInput"];
沒有留言:
張貼留言