-2

I'm using PyCharm if it matters. Whenever I use:

from flask import request

it works, no error.

But whenever I try to use the class like,

request.data
request.args

I get

    raise RuntimeError('working outside of request context')
RuntimeError: working outside of request context

From the command line outside of PyCharm I get,

>>> import flask.request
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named request
>>>

I have seen this and many others,

How to get data received in Flask request

http://flask.pocoo.org/docs/0.10/api/#flask.request

None of the methods or properties are there when I try and import.

I am using a virtual environment, on Windows 7. Everything else in Flask works well.

Community
  • 1
  • 1
johnny
  • 19,272
  • 52
  • 157
  • 259
  • 2
    `request` should be used in a request context. E.g. in a view when handling a request. The documentation is quite clear on this. It is *not* a module. – Martijn Pieters Mar 27 '15 at 17:04
  • 2
    I suggest you start with the [Quickstart](http://flask.pocoo.org/docs/0.10/quickstart/#quickstart) again; it covers things like using `test_request_context()` to create a request context where there is none otherwise. – Martijn Pieters Mar 27 '15 at 17:05
  • 2
    The documentation you link to on [`flask.request`](http://flask.pocoo.org/docs/0.10/api/#flask.request) includes the text: *This is a proxy. See Notes On Proxies for more information*. Did you read that note, and more importantly, the rest of the page that note is on? – Martijn Pieters Mar 27 '15 at 17:06
  • @MartijnPieters Yes, I read it. I evidently didn't understand it. – johnny Mar 27 '15 at 17:15

1 Answers1

1

Flask uses a proxy object for the request. The import from flask import request is correct, but it does not, by itself, represent the request. flask.request is not a module.

Any access to flask.request will look up the current request for you; this makes working with a request simpler, but this also means that you cannot just use it if there is no actual web request going on at the moment.

You'll either have to not use it outside of a view that is actually being called for a request coming in, or you need to explicitly create a request context. You could do that with the Flask.test_request_context() method:

from flask import Flask, request

app = Flask(__name__)

with app.test_request_context('/foo/bar'):
    print request.path  # prints '/foo/bar'

All this is exhaustively documented in the Flask Quickstart and in the Request Context documentation.

You don't have to understand all this. It'll work when you actually have request data to worry about, e.g. when a browser connects to your Flask application and a view function is invoked to render results, request will Just Work.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Martijn, I am trying to get all the requests coming into the webserver, all of them. When I read the documentation it mentioned unit testing. That is not what I am doing. I'm using logging, but I need the IP address of the incoming client which requires the request object. – johnny Mar 27 '15 at 17:20
  • @johnny: Then you probably want to use a [`@app.before_request` handler](http://flask.pocoo.org/docs/0.10/api/#flask.Flask.before_request). It is called for all requests coming in, and at *that time* there is a request context. Also see [*Callbacks and Errors*](http://flask.pocoo.org/docs/0.10/reqcontext/#callbacks-and-errors) in the request context documentation I linked to. – Martijn Pieters Mar 27 '15 at 17:20
  • For production, do most people use the web server to log request, or do they use Flask? I have to have things like bytessent, processingtime, bytesreceived, and more. Thanks for you help, btw. – johnny Mar 27 '15 at 20:06
  • 1
    In production, I'd either use the HTTP server logging facilities (which can give everything you mentioned), or I'd use a dedicated WSGI middleware component; that's what platforms like NewRelic and App Enlight use. – Martijn Pieters Mar 27 '15 at 21:27