2014-05-19 86 views
1

我正在做一個非常基本的POST請求。 將我的requestSerializationMIMEType設置爲RKMIMETypeFormURLEncoded,正如我的服務器預期的那樣(雖然這是默認設置)。方括號之間的Restkit request.body值

現在在日誌中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

請發佈創建正文的代碼。 – danh

+0

@danh我已經更新了這個問題。 –

+0

你還可以顯示'generateJSONMapping'和'User'類(屬性定義)。 – Wain

回答

1

的問題是在RKRequestDescriptor,傳遞rootKeyPath爲空字符串將導致該代碼。 通過nil可以解決問題。

RKRequestDescriptor *requestDescriptor = [ 
RKRequestDescriptor requestDescriptorWithMapping:requestMapping 
objectClass:[User class] 
rootKeyPath:nil 
method:RKRequestMethodPOST]; 
+0

YOU R HERO !!剛剛救了我的命 – iShaalan

相關問題