我正在尝试发布带有附件的对象。
NSMutableURLRequest *request =
[objectManager multipartFormRequestWithObject:reqDocObj
method:RKRequestMethodPOST
path:@"syncDocument.json"
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation([UIImage imageNamed:@"graybox.png"])
name:@"image"
fileName:@"some_file"
mimeType:@"image/jpeg"];
}];
RKObjectRequestOperation *operation =
[objectManager
objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"WS: errore operazione di richiesta %@",error);
}
];
[objectManager enqueueObjectRequestOperation:operation];
objectManager
配置为:
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[编辑]
我的mapepd对象是SynchDocObj
:
requestDocMapping = [RKObjectMapping mappingForClass:[SynchDocObj class]];
[requestDocMapping addAttributeMappingsFromDictionary:mappingDocDict];
问题是:
1)在RKlogs中,request.body = null
和JSON对象被放入表单数据
2)服务器无法解码正文,因为它是null
我的问题是:
1)我是否以错误的方式发送JSON对象?
2)如果是,我如何发送带有文件上传的JSON对象,即作为多部分请求? 此致!
[溶液]
根据答案的建议,我认为解决方案是1)从表单数据而不是正文中检索映射对象; 2)或者发布一个nil对象并在表单数据中放入一个JSON字符串。
答案 0 :(得分:3)
此:
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
是两种不同的方式来说同样的事情 - 而且你不想拥有它们中的任何一种。您需要以表单URL编码(默认值)发送请求。
最简单的方法是使用与当前代码相同的表单来创建请求,先生成JSON,然后使用appendPartWithFormData:name:
将其添加到请求中(就在添加文件之前)
要生成JSON,您可以使用RestKit(RKMappingOperation
),或者您只需创建一个字典/内容数组,然后使用NSJSONSerialization
序列化该对象以添加到请求中。
答案 1 :(得分:0)
分析我的代码,它就像一个魅力:
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[FilledExamCard defineMapping] objectClass:[MappedClassName class] rootKeyPath:nil method:RKRequestMethodPUT];
NSData *jsonPayload = [self getJSONpayloadFromObject:mappedClassObject requestDescriptor:requestDescriptor];
NSURL *baseURL = [NSURL URLWithString:[ZDR_BASE_URL stringByAppendingString:@"PutExamCards"]];
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:baseURL];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];
[client setDefaultHeader:@"Accept" value:@"text/plain"];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
[objectManager setRequestSerializationMIMEType: RKMIMETypeJSON];
[objectManager addRequestDescriptor:requestDescriptor];
NSMutableURLRequest *request = [objectManager multipartFormRequestWithObject:nil method:RKRequestMethodPUT path:@"" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// Name may vary depending from server settings
[formData appendPartWithFormData:jsonPayload name:@"model"];
}];
RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// Process data
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// An error occurred
}];
-(NSData*)getJSONpayloadFromObject:(NSObject*)object requestDescriptor:(RKRequestDescriptor*)requestDescriptor
{
NSDictionary *paramObject = [RKObjectParameterization parametersWithObject:object requestDescriptor:requestDescriptor error:nil];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:paramObject options:NSJSONWritingPrettyPrinted error:&error];
return jsonData;
}