I have an app that uses AFNetworking 2. I am trying to make a request with an Authorization header. Here is the code I have at the moment:
NSURL *baseURL = [NSURL URLWithString:WLAPIBaseURL];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:email password:password];
[manager GET:WLAPIEndpointMe parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
When I make the request, I get a 401 response. I monitored the request with Charles, and it turns out that the Authorization header is not included in the request...
I am sure this is not a problem with the server because when I make the same request with curl from the command-line, it works fine.
So, what am I doing wrong?
I managed to make it work, by using credentials instead of setting the Authorization headers explicitly:
NSURL *baseURL = [NSURL URLWithString:WLAPIBaseURL];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];
NSURLCredential *credential = [NSURLCredential credentialWithUser:email
password:password
persistence:NSURLCredentialPersistenceNone];
manager.credential = credential;
[manager GET:WLAPIEndpointMe parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
I still do not understand why the headers were stripped out though...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments