title: How to return 400 (Bad Request) on Flask?
body: <p>I have created a simple flask app that and I'm reading the response from python as:</p>
  <pre><code>response = requests.post(url,data=json.dumps(data), headers=headers ) 
  data = json.loads(response.text)
  </code></pre>
  <p>Now my issue is that under certain conditions I want to return a 400 or 500 message response. So far I'm doing it like this:</p>
  <pre><code>abort(400, 'Record not found') 
  #or 
  abort(500, 'Some error...') 
  </code></pre>
  <p>This does print the message on the terminal:</p>
  <p><a href="https://i.stack.imgur.com/7YkBw.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7YkBw.png" alt="enter image description here" /></a></p>
  <p>But in the API response I kept getting a 500 error response:</p>
  <p><a href="https://i.stack.imgur.com/OKnXg.png" rel="noreferrer"><img src="https://i.stack.imgur.com/OKnXg.png" alt="enter image description here" /></a></p>
  <p>The structure of the code is as follows:</p>
  <pre><code>|--my_app
     |--server.py
     |--main.py
     |--swagger.yml
  </code></pre>
  <p>Where <code>server.py</code> has this code:</p>
  <pre><code>from flask import render_template
  import connexion
  # Create the application instance
  app = connexion.App(__name__, specification_dir=&quot;./&quot;)
  # read the swagger.yml file to configure the endpoints
  app.add_api(&quot;swagger.yml&quot;)
  # Create a URL route in our application for &quot;/&quot;
  @app.route(&quot;/&quot;)
  def home():
      &quot;&quot;&quot;
      This function just responds to the browser URL
      localhost:5000/
  
      :return:        the rendered template &quot;home.html&quot;
      &quot;&quot;&quot;
      return render_template(&quot;home.html&quot;)
  if __name__ == &quot;__main__&quot;:
      app.run(host=&quot;0.0.0.0&quot;, port=&quot;33&quot;)
  </code></pre>
  <p>And <code>main.py</code> has all the function I'm using for the API endpoints.</p>
  <p>E.G:</p>
  <pre><code>def my_funct():
     abort(400, 'Record not found') 
  </code></pre>
  <p>When <code>my_funct</code> is called, I get the <code>Record not found</code> printed on the terminal, but not in the response from the API itself, where I always get the 500 message error.</p>