Search code examples
pythonflaskrestflask-caching

Flask Endpoint returning 404 not found


I am experimenting with a Flask App to return some json data that is cached with Flask-caching.

This end point works, as well as the flask-caching to cache the get_payload without having to re-run the function:

@app.route('/payload/current', methods=['GET'])
def event_state_current():
    return get_payload()  

But this endpoint does not work at all, the app will return a URL NOT FOUND:

@app.route('/payload/hourly,', methods=['GET'])
def event_state_hourly():
    return get_future()  

Is there anything wrong that I am doing with the flask-caching that is causing this? The endpoint that works does what I was expecting flask-caching to do is only run the get payload function every 60 seconds, but I cant figure out why the /payload/hourly doesnt work at all. Its almost like the flask-caching only supports one endpoint, unless I am doing something wrong.

This is the Flask App for anyone to try, it just makes up data but I am not running it on localhost.

from flask import Flask, request, jsonify
from flask_caching import Cache
import datetime, pytz, random


tz = pytz.timezone('America/Chicago')

cache = Cache()
app = Flask(__name__)
cache.init_app(app, config={'CACHE_TYPE': 'SimpleCache'})


@cache.cached(timeout=60, key_prefix='get_payload')
def get_payload():
        utc_time = datetime.datetime.utcnow()
        utc_time = utc_time.replace(tzinfo=pytz.UTC)   
        corrected_time = utc_time.astimezone(tz) 
        randnum = random.randint(1,1000)
        response_obj = {'status':'success','server_time_corrected':str(corrected_time),'timezone':str(tz),'payload':randnum}
        print(response_obj)   
        return jsonify(response_obj), 200   


@cache.cached(timeout=300, key_prefix='get_future')
def get_future():
        utc_time = datetime.datetime.utcnow()
        utc_time = utc_time.replace(tzinfo=pytz.UTC)   
        corrected_time = utc_time.astimezone(tz) 
        randnum = random.randint(1,1000)
        response_obj = {'status':'success','server_time_corrected':str(corrected_time),'timezone':str(tz),'payload':randnum}
        print(response_obj)   
        return jsonify(response_obj), 200   



@app.route('/payload/current', methods=['GET'])
def event_state_current():
    return get_payload()    



@app.route('/payload/hourly,', methods=['GET'])
def event_state_hourly():
    return get_future()   


if __name__ == '__main__':
    app.run(debug=False,port=5000,host='0.0.0.0')

EDIT, screen snips for testing Flask App Endpoints, this GET request works:

enter image description here

This endpoint doesnt work, any ideas to try? enter image description here


Solution

  • There is a trailing comma in your last URL: '/payload/hourly,'

    Just remove it and it should work.