5

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.

1 Answer 1

8

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.

1
  • 1
    ah, the % port there was a typo; I removed it. I figured it out. The thing that was causing a bad request error was that instead of passing in headers={"content-type": "application/json"}, I needed to pass in content_type="application/json".
    – limp_chimp
    Sep 25, 2013 at 18:09

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.