我正在努力让AFMultipartFormData工作几个小时...... 我需要通过发送他的信息和他的照片来注册用户。 我从后端开发人员那里获得的文档是:
所需的部分是:'注册',内容类型'application / json'和'pic',内容类型为image / *
我尝试了几种方法来做到这一点而没有任何成功。下面的代码有什么问题?
-(void)serverAuthentication {
NSDictionary *jsonDictionary = @{@"clientId":@"someClientId3", @"name":@"Pantelis", @"email":@"someuser@gmail.com", @"password":@"1234567"};
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"sampleJohnTsioris"], 0.5);
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];
if (!jsonData) {
NSLog(@"JSON ERROR");
} else {
NSString *JSONString = [[NSString alloc] initWithBytes:[jsonData bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
NSLog(@"JSON OUTPUT: %@",JSONString);
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager POST:@"http://someserver.com/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:jsonData name:@"registration"];
[formData appendPartWithFileData:imageData name:@"pic" fileName:@"photo.jpg" mimeType:@"image/*"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation=%@",operation);
NSLog(@"Response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"operation=%@",operation);
NSLog(@"Error: %@", error);
}];
}
我得到的错误:
错误Domain = com.alamofire.error.serialization.response Code = -1011 “请求失败:不支持的媒体类型(415)”
答案 0 :(得分:0)
您无法更改请求的内容类型。它必须是“multipart/form-data”。
您需要确保“注册”部分的mime类型为“application / json”。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://someserver.com/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:jsonData name:@"registration" fileName:@"user.json" mimeType:@"application/json"];
[formData appendPartWithFileData:imageData name:@"pic" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation=%@",operation);
NSLog(@"Response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"operation=%@",operation);
NSLog(@"Error: %@", error);
}];
答案 1 :(得分:0)
我找到了解决方案。正如Jeffery Thomas所写,您无法更改请求的内容类型,并且存在问题。具体来说,发送图像时没有问题,但是json对象需要名称&#34; registration&#34;和内容类型,我无法找到通过两者的正确方法。所以,我编写了所需的标题(传递内容类型和名称)。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:@"http://vext.dsigned.gr:8080/auth/account/email" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSMutableDictionary *headers = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"application/json; charset=UTF-8",@"Content-Type", @"form-data; name=\"registration\"",@"Content-Disposition", nil];
[formData appendPartWithHeaders:headers body:jsonData];
[formData appendPartWithFileData:imageData name:@"pic" fileName:@"avatar.png" mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation=%@",operation);
NSLog(@"Response: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"operation=%@",operation);
NSLog(@"Error: %@", error);
}];