IOS and RestKit: how to get text / html response?

I have tried some StackOverflow questions and am going to find the right answer to this. I am using the POSTMAN plugin for Chrome to test my REST calls, and I cannot understand why I cannot read the answer. In the comments, you will see all the various attempts I made to get an answer.

NSDictionary* session_params = @{SESSION_USERNAME_KEY:SESSION_USERNAME_VALUE, SESSION_PASSWORD_KEY:SESSION_PASSWORD_VALUE};
NSURL* url = [NSURL URLWithString:SESSION_URL];
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url];
//GET THE **** THING TO INTERPRET A TEXT response

//[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeTextXML];
//[objectManager setAcceptHeaderWithMIMEType:@"text/html"];
//[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeTextXML];
//[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/html"];
//[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];
//[objectManager setRequestSerializationMIMEType:@"text/html"];

//END
NSMutableURLRequest* request = [objectManager requestWithObject:nil method:RKRequestMethodPOST path:SESSION_URL parameters:session_params];


RKObjectRequestOperation* operation = [objectManager
                                       objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation* operation, RKMappingResult* result)
                                       {
                                           NSLog(@"RESULT [%@]", result);
                                       }
                                       failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                           NSLog(@"ERROR [%@]", error);
                                       }];

    [operation start];

I think the most annoying is that the material I need is contained in the NSLocalizedRecoverySuggestion value. This is the session key I need.

CONCLUSION:

E restkit.network:RKObjectRequestOperation.m:547 : HTTP- : Error Domain = org.restkit.RestKit.ErrorDomain Code = -1016 " {(     " /-WWW--urlencoded ",     " /JSON ")}, /html " UserInfo = 0x1c52aed0 {NSLocalizedRecoverySuggestion = eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJCbG8uUmVnQWxlcnQuQnJva2VyIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdC9CbG8uUmVnQWxlcnQuQVBJL2FwaSIsIm5iZiI6MTM5MjY0MTY2MSwiZXhwIjoxMzkyNjQ1MjYxLCJ1bmlxdWVfbmFtZSI6IkJ1dHRvbnMiLCJyb2xlIjoiUmVnQWxlcnRDb25zdW1lciJ9.JCTMGJRKlOxEtNrcGodpce-tqsRS4zlApNisKQW6iSw, AFNetworkingOperationFailingURLRequestErrorKey =, NSErrorFailingURLKey = HTTP://..., NSLocalizedDescription = {(     "/-WWW--urlencoded ",     " /JSON " )}, /html, AFNetworkingOperationFailingURLResponseErrorKey =} 2014-02-17 14: 54: 20.808 AppName [5600: 6403] E restkit.network:RKObjectRequestOperation.m:213 POST 'http://...' (200 OK/0 ) [request = 0.0000s mapping = 0.0000s total = 0.1925s]: Error Domain = org.restkit.RestKit.ErrorDomain Code = -1016 " {(     " /-WWW--urlencoded ",     " /JSON ")}, /html " UserInfo = 0x1c52aed0 {NSLocalizedRecoverySuggestion = eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJCbG8uUmVnQWxlcnQuQnJva2VyIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdC9CbG8uUmVnQWxlcnQuQVBJL2FwaSIsIm5iZiI6MTM5MjY0MTY2MSwiZXhwIjoxMzkyNjQ1MjYxLCJ1bmlxdWVfbmFtZSI6IkJ1dHRvbnMiLCJyb2xlIjoiUmVnQWxlcnRDb25zdW1lciJ9.JCTMGJRKlOxEtNrcGodpce-tqsRS4zlApNisKQW6iSw, AFNetworkingOperationFailingURLRequestErrorKey =, NSErrorFailingURLKey = HTTP://..., NSLocalizedDescription = {(     "/-WWW--urlencoded ",     " /JSON " )}, /html, AFNetworkingOperationFailingURLResponseErrorKey =}

, , . , RestKit , RestKit, , , , :

NSDictionary* session_params = @{SESSION_USERNAME_KEY:SESSION_USERNAME_VALUE, SESSION_PASSWORD_KEY:SESSION_PASSWORD_VALUE};

NSURL* url = [NSURL URLWithString:SESSION_URL];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:SESSION_URL parameters:session_params];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString* response = [operation responseString];
    NSLog(@"response: %@",response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"error: %@", [operation error]);
}];
[operation start];
+1
2

:

" {(" application/x-www-form-urlencoded "," application/json ")}, /html "

, RestKit form-urlencoded json, html.

, setAcceptHeaderWithMIMEType Mime JSON, , . , , RestKit.

RestKit JSON/XML . , . . , RestKit, AFNetworking ( , RestKit .

+3

Wain Quintin, :)

, Restkit AFNetworking. AFNetworking, , json, plain/text. , .

:

-(void) find_some_token_with_success:(void (^)(AFRKHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFRKHTTPRequestOperation *operation, NSError *error))failure {

    NSURL *baseURL = [NSURL URLWithString:@"https://example.com"];

    AFRKHTTPClient *client = [AFRKHTTPClient clientWithBaseURL:baseURL];
    [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
    [client setDefaultHeader:@"some_custom_header" value:@"some_custom_value"];

    NSMutableURLRequest *request = [client requestWithMethod:@"GET" path:@"/api/v1/some_non_json_endpoint" parameters:nil];

    AFRKHTTPRequestOperation *operation = [[AFRKHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:success failure:failure];
    [operation start];
}

- , , :

-(void) get_the_token:(void (^)(NSString *token))withTokenCallback failure:(void (^)(AFRKHTTPRequestOperation *operation, NSError *error))failure {
    [self xsrftoken_with_success:^(AFRKHTTPRequestOperation *operation, id responseObject) {
        NSString *token = [self get_the_token_from_response:[operation response]];
        withTokenCallback(token);
    } failure:failure];
}

-(NSString *) get_the_token_from_response: (NSHTTPURLResponse *) response;
{
    NSDictionary *headerDictionary = response.allHeaderFields;
    NSString *token = [headerDictionary objectForKey:@"SOME-TOKEN-KEY"];
    return token;
}

, :

- (void)testGetSometokenInARequest
{
    XCTestExpectation *expectation = [self expectationWithDescription:@"Query timed out."];

    [[SomeRequestWithoutJsonResponse alloc]
     get_the_token:^(NSString *token) {
         [expectation fulfill];
         NSLog(@"token: %@", token);
         // this token should be 100 characters long
         XCTAssertTrue([token length] == 100);
     }
     failure:^(AFRKHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"error: %@", [operation error]);
     }];

    [self waitForExpectationsWithTimeout:10.0 handler:nil];
}

, get_the_token .

, - <RestKit/RestKit>, Restkit AFNetowkring:)


restkit:

RestKit: response.body?

Mimetype, :

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];
+1

All Articles