将url参数添加到RestKit RKObjectManager putObject

时间:2013-07-30 04:27:35

标签: objective-c rest restkit restkit-0.20

我正在尝试使用以下格式发送PUT请求:

http://server.com/api/tag/:tagId?extra1=abc&extra2=def

 RKObjectManager *manager = [RKObjectManager managerWithBaseURL:@"http://server.com"];

 [manager.router.routeSet addRoute:[RKRoute routeWithClass:[Tag class] pathPattern:@"/api/tag/:tagId" method:RKRequestMethodPut]];

Tag *tag = [[Tag alloc] initWithName:name andId:tagId];
[manager putObject:tag path:nil parameters:@{@"extra1":@"abc", @"extra2":@"def"}
           success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
               [delegate onRequestSuccess:request withData:tag];
           }
           failure:^(RKObjectRequestOperation *operation, NSError *error) {
           }];

[delegate onRequestStart:request];

但是参数会被放入请求的主体而不是像GET或DELETE请求中那样放入URL。 POST请求也是如此。

感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

以下是我过去解决这个问题的方法:

不是制作路线,只需手动创建URL路径,直接将参数输入字符串:

NSString *urlPath = [NSString stringWithFormat:
                      @"/api/tag/:tagId?extra1=%@&extra2=%@", @"abc", @"def"];

然后,使用putObject而不是path来呼叫您的parameters请求:

[manager putObject:tag path:urlPath parameters:nil
           success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
               [delegate onRequestSuccess:request withData:tag];
           }
           failure:nil];

我不确定这是否是“正确”的做法,但它绝对适合你!