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];
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With