Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorization header not sent using AFNetworking

I'm developing an iOS app that makes frequent requests to a web server, and I'm using AFNetworking (which I very much like). However, I'm running into a problem with authorization that I just can't solve.

The server requires me to provide an authorization header in order to get the response that I want. Specifically, the authorization headers should be like so:

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

I'm using AFNetworking throughout the project, and everything works fine, except for this authorization issue. I'm using the following code:

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

where myClient points to an AFHTTPClient object. Strange enough, when I log the request in XCode using AFHTTPRequestOperationLogger, the logger claims that I have the correct headers set. However, the authorization header does not seem to reach the server - I can't see it in the server log.

To isolate the problem, I've tried to make the exact same request using good old NSURLRequest, as well as curl, and the requests library in Python - all of these work fine, i.e. the authorization header is sent & received (i.e. I can see it in the server log), and the server response is what it should be.

If anyone has run into the same problem (and has found a solution) I would very much appreciate to hear from you.

Thanks.

like image 628
marcel salathe Avatar asked Sep 12 '12 00:09

marcel salathe


1 Answers

Sometimes (especially with Django) this is caused by redirection stripping of header parameters. For instance, /Object redirects to /Object/ in the background and removes the necessary auth parameter during the switch.

If you're using AFNetworkActivityLogger with level AFLoggerLevelDebug then you should be able to check this out in the console. If you see a POST request with /Object and the response with /Object/ then this might indicate redirection stripping is taking place.

like image 121
PF1 Avatar answered Oct 16 '22 08:10

PF1