Forums

Not able to get request headers in python api

Hi

I have deployed my flask api on pythonanywhere. It is working fine. In order to call an api, I need to pass in the request headers which are below:

app_name: myapp

app_key : some_key

Below is the code:

@app.route('/api/app/get_id', methods=['POST'])
def get_id():
    try:
        if 'app_name' and 'app_key' in request.headers:
            """
            SOME CODE
            """

        else:
            return jsonify({"status": "unauthorized", "error": "authentication parameters missing"}), 401

From above code you can see that if app_name and app_key are not present as request headers then we simply throw error. Now I am calling this API from postman:

enter image description here

(If image not showing, use this url: https://ibb.co/j5g9Zjm )

So I am including the app_name and app_key in postman call as headers but I am always getting error message that authentication parameters missing. I am not sure why it is happening as on local testing it was working fine.

For debugging, I also put the logging in the api code like below:

@app.route('/api/app/get_id', methods=['POST'])
def create_gallery():
    try:
        log.error(request.headers)    ## <-- logging
        if 'app_name' and 'app_key' in request.headers:
            """
            SOME CODE
            """

        else:
            return jsonify({"status": "unauthorized", "error": "authentication parameters missing"}), 401

But in the log file also there was not app_name and app_key.

Can anyone please help me here and let me know what I am missing here. Please help. Thanks

isnt't that the wrong syntax?

'app_name' and 'app_key' in request.headers:

perhaps postman is not adding those headers?

I dont think its wrong syntax. If it is wrong syntax then what is the right syntax.? Also I tested this locally and in local testing postman was adding those headers.?

Just tried with python requests, its same. Do you know how to resolve.?

Are you checking the right attribute for request

see here for some info get data flask request

request.headers is a correct attribute.

The code

'app_name' and 'app_key' in request.headers

...doesn't mean what you think it means. It is equivalent to this:

'app_name' and ('app_key' in request.headers)

-- that is, it will evaluate to True if 'app_key' is in request.headers (which may be true) and if the string 'app_name' evaluates to True (which being a non-empty string, it always will).

What you should have is this:

'app_name' in request.headers and 'app_key' in request.headers

But in the log file also there was not app_name and app_key.

So while the if loop may fail it looks like the variables app_name and app_key haven't been passed/read

This has been resolved. I dont know why but after renaming those headers to AppName and AppKey, it started working fine. We can close this.

@rocketsystems Headers with special characters are ignored. That was your issue. This isn't exclusive to pythonanywhere.