Skip to content
Advertisement

Flask POST request is causing server to crash

I am trying to make a simple api in Flask, the first step being getting the POST json data. (I just want to print it for now) This is my code and when I request /api with json data, it returns a 500 error. Any thoughts on why this is happening?

JavaScript

The curl command:

JavaScript

body.json:

JavaScript

Advertisement

Answer

First what you want to do is enable debug mode so Flask will actually tell you what the error is. (And you get the added benefit of flask reloading every time you modify your code!)

JavaScript

Then we find out our error:

JavaScript

You’re returning request.json, which is a dictionary. You need to convert it into a string first. It’s pretty easy to do:

JavaScript

There you are! :)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement