ozba
ozba

Reputation: 6643

How to do in Restkit a PUT request with no body, url params, and get back an object

Lets say that I have to do this dynamic PUT request: "http://mydomain.com/api/5?value=66"

The body is empty. I will get in return a 201 (Created) status, and in the body I'm getting back a json object, Let's call it MyObject that has fields NSNumber* Id, NSString* name;

Now in restkit I have these options:

- [[RKObjectManager sharedManager]  putObject:nil mapResponseWith:MyMapping delegate:self]; 

MyMapping maps MyObject. The problem is that if I'm sending nil, it doesn't know the mapping and throws "Unable to find a routable path for object of type '(null)' for HTTP Method 'PUT'"

- [[RKClient sharedClient] put:putUrl params:nil delegate:self];

where putUrl = "http://mydomain.com/api/5?value=66" The problem here is that there is no mapping for the response so only didLoadResponse is called back and didLoadObjects never called

[objectManager.router routeClass:[MyObject class] toResourcePath:putUrl forMethod:RKRequestMethodPUT]; 
    MyObject *obj = [[MyObject alloc] init];

    [[RKObjectManager sharedManager]  putObject:obj mapResponseWith:MyMapping  delegate:self];

The problem here is that first that I fake it (send MyObject as param while it isn't) and it works only for the first time. for the second time I'm trying to use this method I'm getting this exception: "A route has already been registered for class 'MyObject' and HTTP method 'PUT'"

Any suggestion what to do?

Thanks

Upvotes: 0

Views: 819

Answers (1)

ozba
ozba

Reputation: 6643

If anyone is intersted I found the answer after seeing what restkit is doing.

putUrl = "http://mydomain.com/api/5?value=66";

MyMapping maps the returned MyObject that has fields NSNumber* Id, NSString* name;

Here is the code to get it working:

void (^blockLoader)(RKObjectLoader *);

blockLoader = ^(RKObjectLoader *loader) {
    loader.delegate = self;
    loader.objectMapping = MyMapping;
};

NSString *resourcePath = putUrl;

[[RKObjectManager sharedManager] sendObject:nil toResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
    loader.method = RKRequestMethodPUT;
    blockLoader(loader);
}];

Upvotes: 3

Related Questions