Get HTTP Authenticated JSON with AFNetworking

I am trying to get JSON from my hudson url and authenticate the application (Mac OS X) using HTTP Authenticate.

Following the example I'm using:

// AppDelegate.m
- (void) doSomething {
    [[CommAPIClient sharedClient] getPath:@"/computer/api/json" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Name: %@", [responseObject valueForKeyPath:@"totalExecutors"]);

    } failure:nil];
}

// CommAPIClient.m
+ (CommAPIClient *) sharedClient {
    static CommAPIClient *_sharedClient = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:    [appDelegate.hudsonTextField stringValue]]];
    });
    return _sharedClient;
}

- (id) initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self){
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

        AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
        userName = [appDelegate.userTextField stringValue];
        password = [appDelegate.passwdTextField stringValue];

        [self setAuthorizationHeaderWithUsername:userName password:password];
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }
    return self;
}

I want the list of computers to appear in my drop-down list, but these two lines do not work together: [self setAuthorizationHeaderWithUsername: userName password: password]; [self setDefaultHeader: @ "Accept" value: @ "application / json"];

If I just use the first line, my autoresetting works, but I get this error because I'm trying to get the key:

2012-02-03 02:43:57.542 HudsonSlave[7523:707] An uncaught exception was raised
2012-02-03 02:43:57.542 HudsonSlave[7523:707] [<NSConcreteData 0x100850000> valueForUndefinedKey:]: this class is not key value coding-compliant for the key totalExecutors.

2012-02-03 02:43:57.623 HudsonSlave[7523:707] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSConcreteData 0x100850000> valueForUndefinedKey:]: this class is not key value coding-compliant for the key totalExecutors.'

If the second line is used, my authentication will return a 403 error.

Anyone can help with the problem?

Thank you and apologize for any errors in English.

Thiago

+5
5

API Flickr AFJSONRequestOperation, . defaultAcceptableContentTypes, :

+ (NSSet *)defaultAcceptableContentTypes;
{
    return [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain", nil];
}

AFHTTPClient HTTP-:

[self registerHTTPOperationClass:[CCFFlickrJSONRequestOperation class]];

2013-09-09 14-52-47:

+ (NSSet *)acceptableContentTypes

+14

" {(" text/javascript "," application/json "," text/json ")}, /javascript "

, text/javascript, application/json text/json, application/javascript ( mime JSON).

mime, (, ) AFJSONRequestOperation, , mime . , , , , , .

+7

:

[AFHTTPRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];
+5

, AFJSONRequestOperation, , :

AFJSONRequestOperation *operation = blah blah blah

 [operation setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/plain",@"text/html", nil]];

[operation start];
+4

AFNetworking 2.0, RequestOperation Serializer. 2.0, - ( , , ContentTypes - , AFNetworking SDK):

AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];

NSMutableSet *contentTypes = [requestManager.responseSerializer.acceptableContentTypes mutableCopy];
[contentTypes addObject:@"text/html"];
[contentTypes addObject:@"text/plain"];
// ... etc ...
requestManager.responseSerializer.acceptableContentTypes = [contentTypes copy];

If you decide to use the AFHTTPRequestOperation object directly, simply replace "requestManager.responseSerializer" with "myOperationObject.responseSerializer".

+2
source

All Articles