Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Documentation for Flask app object `get` and `post` class methods?

Tags:

python

flask

get

In the Flask documentation on testing (http://flask.pocoo.org/docs/testing/), it has a line of code

rv = self.app.get('/')

And below it, it mentions "By using self.app.get we can send an HTTP GET request to the application with the given path."

Where can the documentation be found for these direct access methods (I'm assuming that there's one for all of the restful methods)? Specifically, I'm wondering what sort of arguments they can take (for example, passing in data, headers, etc). Looking around on flask's documentation for a Flask object, it doesn't seem to list these methods, even though it uses them in the above example.

Alternatively, a knowledgeable individual could answer what I am trying to figure out: I'm trying to simulate sending a POST request to my server, as I would with the following line, if I were doing it over HTTP:

    res = requests.post("http://localhost:%d/generate" % port, 
                        data=json.dumps(payload), 
                        headers={"content-type": "application/json"})

The above works when running a Flask app on the proper port. But I tried replacing it with the following:

    res = self.app.post("/generate", 
                        data=json.dumps(payload), 
                        headers={"content-type": "application/json"})

And instead, the object I get in response is a 400 BAD REQUEST.

like image 473
limp_chimp Avatar asked Sep 25 '13 16:09

limp_chimp


People also ask

How do you handle GET and POST IN Flask?

POST Method To handle the POST requests at the server, let us first create a form to get some data at the client side from the user, and we will try to access this data on the server by using the POST request. Now, Enter the following code into the script named post_example.py.

How do you POST a method in Flask?

request. post() method is used to generate a POST request. This method has can contain parameters of URL, params, headers and basic authentication. URL is the location for sending the request.

How do I get data from a Flask POST request?

Inside the view function, you will need to check if the request method is GET or POST. If it is a GET request, you can display the form. Otherwise, if it is a POST request, then you will want to process the incoming data. Fill out the language field with value of Python and the framework field with the value of Flask .

How do I get POST JSON data in Flask?

Alternatively, you can use the request. get_json() method. Both accessing the field itself and the method returns a dict - with key-value pairs present in the incoming JSON. Note: The json field and get_json() methods will only work if the Content-Type of the POST request is set to application/json .


1 Answers

This is documented in the Werkzeug project, from which Flask gets the test client: Werkzeug's test client.

The test client does not issue HTTP requests, it dispatches requests internally, so there is no need to specify a port.

The documentation isn't very clear about support for JSON in the body, but it seems if you pass a string and set the content type you should be fine, so I'm not exactly sure why you get back a code 400. I would check if your /generate view function is invoked at all. A debugger should be useful to figure out where is the 400 coming from.

like image 180
Miguel Avatar answered Sep 25 '22 23:09

Miguel