Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get JSON with HTTP Authentication with AFNetworking

I am trying to get a JSON from my hudson url address and authenticate my (Mac OS X) application using the 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 to get the computer's list to show in my dropdown, but this two lines does not work together: [self setAuthorizationHeaderWithUsername:userName password:password]; [self setDefaultHeader:@"Accept" value:@"application/json"];

If I just use the first line, my authetication works, but I receive that error because I try to get a 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 use the second line, my authentication will return an error 403.

Anyone could help with problem ?

Thanks and apologize for any errors in english.

Thiago

like image 284
unnamedd Avatar asked Feb 03 '12 04:02

unnamedd


2 Answers

I ran into this issue with the Flickr API and ended up subclassing AFJSONRequestOperation which turned out to be trivial. You need only override the class method defaultAcceptableContentTypes thusly, for example:

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

Then in my AFHTTPClient subclass, I just register my subclass as the default HTTP operation class:

[self registerHTTPOperationClass:[CCFFlickrJSONRequestOperation class]];

UPDATE 2013-09-09 14-52-47:

The method name is now + (NSSet *)acceptableContentTypes

like image 123
FluffulousChimp Avatar answered Oct 16 '22 15:10

FluffulousChimp


"Expected content type {( "text/javascript", "application/json", "text/json" )}, got application/javascript"

Just like the error said, the request was expecting one of text/javascript, application/json, or text/json, but the server sent application/javascript (which is an incorrect mime type for JSON).

Either get your server to respond with the correct mime type, or (perhaps easier) manually change the line in AFJSONRequestOperation that specifies which mime types are accessible. There's a less hacky alternative to changing the library code that involves subclassing, but that's probably much more trouble than it's worth in your case.

like image 22
mattt Avatar answered Oct 16 '22 16:10

mattt