53

The answer that I found on the web is to use request.args.get. However, I cannot manage it to work. I have the following simple example:

from flask import Flask
app = Flask(__name__)

@app.route("/hello")
def hello():
    print request.args['x']
    return "Hello World!"

if __name__ == "__main__":
    app.run()

I go to the 127.0.0.1:5000/hello?x=2 in my browser and as a result I get:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

What am I doing wrong?

Roman
  • 97,757
  • 149
  • 317
  • 426
  • 1
    it's about half way down the [quickstart page under Accessing Request Data](http://flask.pocoo.org/docs/0.10/quickstart/#accessing-request-data) under [context locals](http://flask.pocoo.org/docs/0.10/quickstart/#context-locals) – Mark Mikofski Mar 19 '15 at 23:04
  • 1
    what if I don't know what keys the user is sending in the request? and the headers? – devssh Dec 11 '18 at 14:18

2 Answers2

90

The simple answer is you have not imported the request global object from the flask package.

from flask import Flask, request

This is easy to determine yourself by running the development server in debug mode by doing

app.run(debug=True)

This will give you a stacktrace including:

print request.args['x']
NameError: global name 'request' is not defined
sberry
  • 113,858
  • 17
  • 127
  • 157
10

http://localhost:5000/api/iterators/opel/next?n=5

For something like the case before

from flask import Flask, request
n = request.args.get("n")

Can do the trick

Joe9008
  • 543
  • 5
  • 11