33

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?

from flask import Flask, request, Response
app = Flask(__name__)

@app.route('/')
def root_response():
    return "Hello World."

@app.route('/api', methods=['POST', 'GET'])
def api_response():
    if request.method == 'POST':
        return request.json

if __name__ == '__main__':
    app.run()

The curl command:

$ curl -H "Content-Type: application/json" --data @body.json http://127.0.0.1:5000/api
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

body.json:

{
"please": "print",
"me": "now"
}

4 Answers 4

51

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!)

if __name__ == '__main__':
    app.debug = True
    app.run()

Then we find out our error:

TypeError: 'dict' object is not callable

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

def api_response():
    from flask import jsonify
    if request.method == 'POST':
        return jsonify(**request.json)

There you are! :)

3
  • 4
    That debug mode saved me a lot of time :D Jun 15, 2015 at 7:40
  • 1
    Are the other settings for debug, because I find that despite having debug set to True, I don't get the error messages on screen.
    – disruptive
    Sep 2, 2015 at 17:53
  • I actually realized that all error messages were output to the terminal where I was running my server. Yet the debug mode turned out to be helpful for other testings (and it prints right where you run your curl command)
    – alisa
    Jan 17, 2017 at 19:35
6

The server is overloaded because the default port(5000) or port explicitly mentioned by a user(eg:app.run(port=7000)) may be using some other processes in the background, so we need to kill the processes which are being used by that port.

You can see the process id's(PIDS) which are using that port by using the following command: netstat -o -a in command prompt enter image description here *Look into the respective PID for the port

Then kill all the Processes(PIDS) for the port you want to use using the following command: Taskkill /PID 30832 /F Here I used the PID 30832 for port 127.0.0.1:7000 which is giving the overloaded error. After that problem is solved.

2

the server is overloaded on the port you are using. if you have a linux server, print the processes bounded to your port like this:

fuser 5000/tcp

and this will kill the processes bounded to it:

fuser -k 5000/tcp

and you're good to go

-1

1.First of all you set :

if __name__ == '__main__':
app.run(debug=TRUE)

2.The server(defaut) is busy in process ,first you terminate that process on that server:

You can see the process id's(PIDS) which are using that port by using the following command: netstat -o -a in command prompt. Enter here to see sample Look into the corresponding PID for the port.

Then kill all the Processes(PIDS) for the port you want to use using the following command: Taskkill /PID 10324 /F Here I used the PID 10324 for port 127.0.0.1:5000 which is giving the overloaded error. After that problem is solved.

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.