K
K
keddad2019-08-05 21:07:02
Python
keddad, 2019-08-05 21:07:02

Can Flask/Flask Restful return HTTP 200 without being explicitly told?

I have an API method that can return (described in Return) only 2 HTTP response codes: 201 and 400. Also, I can expect HTTP 500 if something goes wrong. But when passing the tests, the API returns HTTP 200 instead of 400. How can this happen? Even when I add something like

if True:
            return 400

It still returns 200! How can this happen?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lightmanLP, 2019-08-05
@lightmanLP

I understand you are using Flask. https://www.flaskapi.org/api-guide/status-codes/

K
keddad, 2019-08-05
@keddad

Okay, I found the problem. If the response contains only HTTP code, it looks like
Unfortunately, flask-restful does not handle it correctly. This results in a response with the response text "400" and HTTP code - 200. The way out is to manually create the flask.Responce object :
return Responce(status=400)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question