Reputation: 1753
I have an extremely bare bones python REST application:
from app import app
def main():
parser = ArgumentParser(description="Character information hosting server!")
parser.add_argument('--parse-word-doc', dest="parse_doc", required=False, action='store_true',
help='Parses a word document for characters and then serializes them with pickle to a file')
parser.add_argument('--source-file', dest="source_file", type=str, required=False,
help='Path and name of the source word document you want to read from. Defaults to source.docx.',
default="source.docx")
parser.add_argument('--port', dest="port", required=False, type=int, default=5000,
help='Specify the port you want Flask to run on')
parser.add_argument('--log-level', metavar='LOG_LEVEL', dest="log_level", required=False, type=str, default="info",
choices=['debug', 'info', 'warning', 'error', 'critical'],
help='The log level at which you want to run.')
args = parser.parse_args() # type: argparse.Namespace
if not Path(args.source_file).is_file():
logging.error("Could not find " + args.source_file + ". Are you sure you got the path right?")
exit(1)
logging.info("Reading data from the file \"database\" from disk")
with open('database', 'rb') as database:
characters = pickle.load(database)
app.config['DATABASE'] = characters
if args.log_level:
if args.log_level == "debug":
logging.basicConfig(level=logging.DEBUG)
app.config['DEBUG'] = True
elif args.log_level == "info":
logging.basicConfig(level=logging.INFO)
elif args.log_level == "warning":
logging.basicConfig(level=logging.WARNING)
elif args.log_level == "error":
logging.basicConfig(level=logging.ERROR)
elif args.log_level == "critical":
logging.basicConfig(level=logging.CRITICAL)
else:
logging.basicConfig(level=logging.INFO)
app.run(host='0.0.0.0', port=args.port)
if __name__ == '__main__':
main()
from flask import Flask
# Initialize the app
app = Flask(__name__)
# Load the views
from app import views
import os
class Config(object):
# Enable Flask's debugging features. Should be False in production
DEBUG = os.environ.get('DEBUG') or True
SECRET_KEY = os.environ.get('SECRET_KEY') or 'default-secret-just-for-csrf-attacks-nbd'
from flask import request
from app import app
@app.route('/api/lookup', methods=['GET'])
def lookup():
input_text = request.args.get('character_to_lookup')
if input_text in app.config['DATABASE']:
return app.config['DATABASE'][input_text]
else:
return {}
If I send a simple curl command: curl -X GET -d '{"character_to_lookup": "test"}' http://127.0.01:5000/api/lookup -H 'Content-Type: application/json'
and check in PyCharm for debugging - request simply doesn't exist at all. It isn't set to none, it just doesn't exist. There must be something about the app context I'm missing, but it isn't clear.
The only thing I've been able to find online is that you need to import request, which I've done and I can't think of another reason that request simply wouldn't exist.
Upvotes: 0
Views: 781
Reputation: 1753
The problem was that request.args in flask refers specifically to data passed via the url. Flask uses different values in request for different types of data. In my case, I wanted JSON so I had to use request.get_json()
.
See this answer for a great explanation.
Upvotes: 1