我正在做一个非常基本的POST请求。
将我的requestSerializationMIMEType
设置为我的服务器所期望的RKMIMETypeFormURLEncoded
(尽管它是默认设置)。
现在在日志中request.body
看起来像这样:
request.body=[param1]=test&[param2]=2724
服务器中产生的未知表单值。
这里的问题是参数名称,它们在方括号之间,我没有解释为什么他们会像这样序列化!
我的代码与github上提供的示例完全相同。
注意:我已经手动完成了POST请求而没有方括号,并且它工作正常。
修改
代码
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GenericResponse class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"error":@"error", @"status":@"status"}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);
RKResponseDescriptor *genericDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"" keyPath:@"" statusCodes:statusCodes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:[User generateJSONMapping]];//dictionary mapping
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"" method:RKRequestMethodPOST];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://foo.com/rest/createUser/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:genericDescriptor];
manager.requestSerializationMIMEType=RKMIMETypeFormURLEncoded;
// POST to create
[manager postObject:user path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"success");// the request is success but the params are not delivered to the server
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"fail");
}];
我猜User
非常简单
@interface User : NSObject
@property NSNumber *idShort;
@property NSNumber *idLong;
@property NSNumber *idCom;
@property NSString *comment;
@property NSString *token;
+ (NSDictionary *) generateJSONMapping;
@end
@implementation User
+ (NSDictionary *) generateJSONMapping
{
return @{
@"idShort": @"idShort",
@"idLong": @"idLong",
@"idCom":@"idCom",
@"comment":@"comment",
@"token":@"token",
};
}
@end
答案 0 :(得分:1)
问题出在RKRequestDescriptor
,传递rootKeyPath
为空字符串会导致此问题。
通过nil
可以解决问题。
RKRequestDescriptor *requestDescriptor = [
RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[User class]
rootKeyPath:nil
method:RKRequestMethodPOST];