I have deployed a flask based python app on PythonAnywhere
. It contains an API which need below request headers:
app_name: <app-name>
app_key: <app-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
except Exception as e:
log.error("Exception occurred {}".format(e))
As you can see that it needs app_name
and app_key
. If not found in headers, then API simply returns error. I am calling this API from postman and have included app_name
and app_key
:
But it always throws error authentication parameters missing
. In my local testing it was working fine. I also tried debugging by putting logging like below:
@app.route('/api/app/get_id', methods=['POST'])
def get_id():
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
except Exception as e:
log.error("Exception occurred {}".format(e))
But app_name
and app_key
were not there in log file as well. It looks like for some reason PythonAnywhere
is not accepting these request headers. Does anyone know what is the issue and how can I resolve it. Please help. Thanks
'app_name' and 'app_key'
evaluates to True in Python, so your if statement is asking if the key True is in the headers and it probably isn't.
Try using if 'app_name' in request.headers and 'app_key' in request.headers'