I am writing an iOS application that interacts with a service to update a user profile. To update the username, the iOS application issues a request with the body in the following format:
{"name" : {"first" : "FIRSTNAMEGOESHERE", "lastName":"LASTNAMEGOESHERE"}}
In the case when the request body is valid (that is, both the first and the last name are provided), the service returns a status code 204 (without content) and an empty response body.
If the request body is invalid (for example, the name is missing, etc.), the service will return a status code of 400 and the response body in the following format:
{"code":"ERRORCODEHERE"}
The iOS application uses RestKit, and I could not figure out how to get it to properly handle success as well as failure. If I use this:
- (void)updateUserName
{
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[[NSURL alloc] initWithString:@"http://someqaserver:8080"]];
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromDictionary:@{@"firstName" : @"first", @"lastName" : @"last"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[OurUser class] rootKeyPath:@"name"];
[objectManager addRequestDescriptor:requestDescriptor];
[objectManager putObject:[OurUser getCurrentUser]
path:@"/mobile/profile/name"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *result)
{
NSLog(@"Success!");
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
NSLog(@"Fail!");
}];
}
, , 204 .
putObject, , 204- :
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[ServiceError class]];
[errorMapping addAttributeMappingsFromDictionary:@{@"code" : @"errorCode"}];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[objectManager addResponseDescriptor:errorDescriptor];
, HTTP , , 204 (, , - ?)
, :
1.) 204,
2.) 4XX, ServiceError?
, .