Problem when getting 'Authorization' header on test Flask application with FlaskClient Problem when getting 'Authorization' header on test Flask application with FlaskClient flask flask

Problem when getting 'Authorization' header on test Flask application with FlaskClient


I believe this may be to do with the way HTTP requests process headers, where they capitalise them and add HTTP_ as a prefix. Try changing your header to HTTP_AUTHORIZATION instead of just Authorization, since the test client will not be setting this properly.


I'm sorry guys for this silly question.Now I've figure the answer out.The problem was that I was trying to do a GET request in a view that is using a POST method.

I've just replaced the request from

response = self.app.get('/sign_in', headers=headers)

to

response = self.app.post('/sign_in', headers=headers)

and now it started to work.

I will let this question here in case of someone gets the same silly error.

Thank you so much.


If similar issue for DRF clients, You can use,

client = APIClient()client.credentials(HTTP_AUTHORIZATION='Token {token}'.format(token=token))

Ref: https://www.django-rest-framework.org/api-guide/testing/#credentialskwargs