Authorization header not sent using AFNetworking

I am developing an iOS application that makes frequent requests to the web server and I use AFNetworking (which I really like). However, I ran into an authorization problem that I simply cannot solve.

The server requires me to provide an authorization header in order to get the response I want. In particular, authorization headers should be:

Authorization = "ApiKey some-user-name:someNumericalApiKey" 

I use AFNetworking throughout the project and everything works fine except for this authorization issue. I am using the following code:

 [myClient setDefaultHeader:@"Authorization" value:@"ApiKey some-user-name:someNumericalApiKey"]; 

where myClient indicates an AFHTTPClient object. Oddly enough, when I register a request in AFHTTPRequestOperationLogger using AFHTTPRequestOperationLogger , the registrar claims that I have the correct headers set. However, the authorization header does not seem to reach the server - I do not see it in the server log.

To isolate the problem, I tried to make the same request using the good old NSURLRequest , as well as curl and requests libraries in Python - all this works fine, that is, the authorization header is sent and received (i.e. I can see it in the server log ), and the server response is what it should be.

If someone ran into the same problem (and found a solution), I would be very grateful for you.

Thanks.

+7
source share
2 answers

Sometimes (especially with Django) this is caused by reconfiguring the header parameters. For example, / Object redirects to / Object / in the background and removes the necessary auth option during the switch.

If you use an AFNetworkActivityLogger with an AFLoggerLevelDebug level, you should check this on the console. If you see a POST request with / Object and a response with / Object /, this may indicate that the redirect was canceled.

+2
source

If you create your operation manually, they are not accepted by default, this may be the cause of your problem.

0
source

All Articles