_unicodify_header_value(self.environ['HTTP_' + key]) gives KeyError: 'HTTP_TOKEN'

Viewed 1185
request.headers['token']

I am getting the token value like this in the flask. But this returns a key error something like:

api       |     return _unicodify_header_value(self.environ['HTTP_' + key])
api       | KeyError: 'HTTP_TOKEN'

the above part is from datastructures.py from werkzueg. Client-side:

...
        retries = Retry(total=total,
                        backoff_factor=backoff_factor,
                        status_forcelist=[429, 500, 502, 503, 504])
        http = requests.Session()
        http.mount("https://",
                   TimeoutHTTPAdapter(max_retries=retries, timeout=timeout))
        http.mount("http://",
                   TimeoutHTTPAdapter(max_retries=retries, timeout=timeout))
        self.http = http
...
...
    def add_regular_user_request(self,
                                 user_name=None,
                                 password=None,
                                 email=None):
        endpoint = "/api/regularuser"
        api_url_complete = self.api_url + endpoint
        params = {}
        if user_name is None or password is None or email is None:
            raise IncompleteParams
        params['user_name'] = user_name
        params['password'] = password
        params['email'] = email
        result = self.http.post(api_url_complete,
                                data=params,
                                headers=self.headers)
...
...

What could be problem? Any other details please let me know.

1 Answers

First, check to confirm you are indeed passing jwt authorization tokens to your flask app. In something like Chrome dev tools, inspect your frontend code request to confirm you are indeed passing in Authorization tokens. Or use postman to set all the auth token headers manually for the specific Flask blueprint route you are trying to test.

Second, confirm that you are not trying to prematurely access the jwt tokens before your route has started handling the request. In other words, do you have any @application.before_request handlers that are trying to intercept or process the jwt tokens? These handlers should be wherever you describe your Flask application at the top most level (the thing that starts your Flask app).

In my case, my error/use case was the result of trying to access jwt auth tokens too early in the route handling process. Your cause might be different from my use case, but the breaking change is likely still be the same. Try commenting out from your @application.before_request handler part of the code that is doing something like get_jwt_identity:

@application.before_request
def before_request_handler():
    g.session_id = uuid4()
    g.user_id = get_jwt_identity() #TODO: comment out this entire line

I can't provide a specific solution without knowing more about your larger application context, but this is definitely what is causing my problem. I'm accessing the jwt tokens before the route has set my Authorization jwt token. In my case, I don't need the Authorization jwt token to successfully set the user_id as a global session variable and the route handler is able to successfully set the Authorization token later, so there isn't technically anything broken here, other than the annoying false error message.

Update: Confirmed that moving my get_jwt_identity() function call out of the before_request handler and lower into a repository type service resolved my error. I am no longer seeing that error.

Related