Calling Flask with requests.Session causes flip-flopping between 500 and 400 response with same API call

Viewed 45

I have a flask app like this

from flask import Flask, request

api = Flask(__name__)


@api.route("/foo", methods=["POST"])
def foo(*args, **kwargs):
    request.get_json()
    return 1 / 0


if __name__ == "__main__":
    api.run(host="0.0.0.0", port=8005, debug=True)

Now if I call this with the following Python code from command line

python -c '
import requests
s=requests.Session()
u="http://localhost:8005/foo"
j={"A":1}
r1=s.post(u, json=j)
r2=s.post(u, json=j)
r3=s.post(u, json=j)
print(r1)
print(r2)
print(r3)'

I get

<Response [500]>
<Response [500]>
<Response [500]>

But if change the flask app to not consume the json, i.e. remove the request.get_json() line, then the requests session will keep flip flopping between a 500 and 400 response, i.e.

<Response [500]>
<Response [400]>
<Response [500]>

It must be something to do with some data flushing but I can't find anything online about it, does anyone have an idea what's happening here?

Obviously this a contrived example, but the point is I'm expecting all 500's regardless of whether or not the JSON is consumed.

0 Answers
Related