Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking doesn't seem to send Authorization Header with AFJSONRequestOperation

I'm trying to send an Authorization header using AFNetworking and AFJSONRequestOperation. If I NSLog the httpClient after setAuthorizationHeaderWithToken, it'll show that it's in the header, but when I check the server it's sending to, it doesn't seem to receive the Authorization header (it receives the other parts).

Does AFJSONRequestOperation do something with the header where it's not adding the Authorization part?

NSURL *url = [NSURL URLWithString:kBaseURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

 NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"/" parameters:params];   

[httpClient setAuthorizationHeaderWithToken:@"test"];

AFJSONRequestOperation *operation = nil;

operation = [AFJSONRequestOperation
             JSONRequestOperationWithRequest:request
             success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

             }
             failure:^(NSURLRequest *request , NSHTTPURLResponse *response, NSError *error , id JSON ){

             }];

[operation start];

like image 644
user1218464 Avatar asked Nov 04 '22 12:11

user1218464


1 Answers

Have you tried adding the header before creating the NSMutableURLRequest (which in this block of code doesn't seem to need to be mutable) with the httpClient? It looks like you're not adding the token header until after you're creating the request in which case the AFJSONRequestOperation never even sees the header, since really in the source code of AFHTTPClient that function is just a convenience method for adding an HTTP header with the name "Authorization"

like image 95
Keith Smiley Avatar answered Nov 09 '22 03:11

Keith Smiley