1

I'm trying to read a simple json string response from a web service :

Suppose I have this mapping :

RKObjectMapping* loginRequestMapping = [RKObjectMapping requestMapping];
[loginRequestMapping addAttributeMappingsFromDictionary:@{
 @"userName" : @"UserName",
 @"password" : @"Password"
 }];
RKRequestDescriptor* loginReq = [RKRequestDescriptor requestDescriptorWithMapping:loginRequestMapping objectClass:[LoginRequest class] rootKeyPath:nil method:RKRequestMethodPOST];
[objectManager addRequestDescriptor:loginReq];

I tried this request :

LoginRequest* loginReq = [LoginRequest new];
loginReq.userName = @"username";
loginReq.password = @"1234567890";

__block NSString* token;
[objectManager postObject:loginReq
                     path:@"/sendboxapi/api/login"
                     parameters:nil
                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                            token = [mappingResult firstObject];
                            NSLog(@"RESULT : %@", token);
                        }
                        failure:^(RKObjectRequestOperation *operation, NSError *error) {
                            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                            message:[error localizedDescription]
                                                                           delegate:nil
                                                                  cancelButtonTitle:@"OK"
                                                                  otherButtonTitles:nil];
                            [alert show];
                            NSLog(@"Hit error: %@", error);
                        }];

The raw response have this format : "47c4e389-be2b-466b-b015-c8d5682c4f0a"

I get this error : Hit error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'"

What is the Correct RKObjectMapping to set to be able to read this string from the webservice ?

Thank you

Aladdin Gallas
  • 701
  • 2
  • 12
  • 36

1 Answers1

0

When you use postObject, RestKit will try to apply the response data to the source object (your LoginRequest). So you need to add somewhere for that data to be stored and a (response) mapping and descriptor to process it.

Also, if the string returned to you is:

"47c4e389-be2b-466b-b015-c8d5682c4f0a"

That isn't JSON and you'll have issues trying to process it as such.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • But how can I use the response in my case ? I receive it correctly but I just need get it from somewhere and avoid this exception ! – Aladdin Gallas Jul 24 '13 at 15:38
  • Change the response to JSON. Or, the header should say that the content type is plain text and then you'll need to setup the serialisation and mappings to handle the plain text. – Wain Jul 24 '13 at 15:45
  • So this is the server fault ... I still think that RestKit is powerful but inflexible since 0.20 ! The older version was very flexible ! Is that possible to make an old style RestKit RKRequest in the 0.20 ? – Aladdin Gallas Jul 24 '13 at 15:51
  • You can make the request operation (not technically `RKRequest`). If the server says it's returning JSON then it should return *valid* JSON... – Wain Jul 24 '13 at 15:58
  • 1
    For anyone else getting here from search, it seems strings are now actually valid JSON responses. http://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json/22361229#22361229 – Danny Sung Aug 07 '15 at 04:04