Documentation for objects of class Flask `get` and` post`?

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

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

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

Where can I find documentation for these direct access methods (I assume there is one for all other methods)? In particular, I wonder what arguments they can take (for example, passing data, headers, etc.). Looking back at the flask documentation for the Flask object, it doesn't seem to list these methods, although it uses them in the example above.

Alternatively, a knowledgeable person may respond to what I am trying to understand: I am trying to simulate sending a POST request to my server, as with the following line, if I were to do this via HTTP:

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

This works when running the Flask application on the appropriate port. But I tried to replace 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 400 BAD REQUEST .

+4
source share
1 answer

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

Client-client does not issue HTTP requests, it sends requests internally, so there is no need to specify a port.

The documentation doesn't say very clearly about JSON support in the body, but it seems that if you pass the string and specify the content type, you should be fine, so I'm not quite sure why you are returning 400 code. I would check if you do call the view function /generate . A debugger should be helpful to find out where the 400 is from.

+7
source

All Articles