AFNetworking upload task with credentials (HTTP Basic Auth)
Asked Answered
G

1

7

I need to upload video files from my app to a server. I tried doing so via [AFHTTPRequestOperationManager post:parameters:success:failure] but unfortunately kept getting request timeouts. I'm now trying something similar to Creating an Upload Task from the AF Docs.

I read on SO and the AF Docs about setSessionDidReceiveAuthenticationChallengeBlock: and tried to implement the whole upload malarky as follows:

__block ApiManager *myself = self;

// Construct the URL
NSString *strUrl = [NSString stringWithFormat:@"%@/%@", defaultUrl, [self getPathForEndpoint:endpoint]];
NSURL *URL = [NSURL URLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];

// Build a session manager
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

// Set authentication handler
[manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing *credential) {
    *credential = myself.credentials;
    return NSURLSessionAuthChallengeUseCredential;
}];


// Create the upload task
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        [myself endpoint:endpoint returnedFailure:error];
    } else {
        [myself endpoint:endpoint returnedSuccess:responseObject];
    }
}];

// and run with it
[uploadTask resume];

The myself.credentials object has been set previously to have the correct username and password. Whenever this request fires, I get 401 unauthorised as a response. I tried putting NSLog(@"CHALLENGE") inside the challenge block above, but it never seems to get called, so AFNetworking isn't giving me a way to supply credentials. I know that this works perfectly well on the server side because I've tested it with Postman.

How can I get AFNetworking to let me supply credentials for HTTP Basic Auth with this upload task?

Gemmulation answered 29/9, 2014 at 19:25 Comment(0)
A
2

I'm not sure about AFNetworking's setSessionDidReceiveAuthenticationChallengeBlock, but as long as you have an NSMutableURLRequest you may set the Authorization HTTP Header directly on the request object:

[request setValue:base64AuthorizationString forHTTPHeaderField:@"Authorization"];

Keep in mind that the value must be base64 representation of the username:password string.

Otherwise, for GET, POST, etc. requests on a session manager, you may set the credentials on the request serializer used by the session manager. See:

[AFHTTPRequestSerializer setAuthorizationHeaderFieldWithUsername:password:] [AFHTTPRequestSerializer clearAuthorizationHeader]

Atonal answered 8/3, 2016 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.