简体   繁体   中英

Reskit response is just a string, not JSON

In my app i am making different calls and they work except one call, that returns just a string in response as SUCCESS. I am getting this error

"The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (200) with content type 'application/json'}

How can i tell the restkit to access the "Content-Type: text/plain"

This is a post call.

Short answer: you can't. RestKit is designed to work with JSON objects only, and so it expects a JSON response (keeping with the RESTful paradigm).

However, you can definitely post objects using AFNetworking, which RestKit actually includes. I use AFNetworking for non-coreData-related correspondence. Here's a code sample on how to get the AFHTTPClient from RestKit and make a POST, expecting a text/plain response.

AFHTTPClient *httpClient = [RKObjectManager sharedManager].HTTPClient;
NSDictionary *requestObject = @{@"label1":data1, @"label2":data2};
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient setDefaultHeader:@"Accept" value:@"text/plain"];
[httpClient postPath:urlPath parameters:requestObject success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = (NSString*)responseObject;
    if([response isEqualToString:@"SUCCESS"]) NSLog(@"It worked!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure code goes here
}];

If that's your only call expecting text/plain , change the Accept header back after you're done:

[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM