Sto provando a creare una semplice API in Flask, il primo passo è ottenere i dati POST json. (Voglio solo stamparlo per ora) Questo è il mio codice e quando richiedo/api con dati json, restituisce un errore 500. Qualche idea sul perché questo sta accadendo?Flask La richiesta POST sta causando il crash del server
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()
Il comando riccio:
$ 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"
}
Questa modalità di debug mi ha risparmiato un sacco di tempo: D –
Sono le altre impostazioni per il debug, perché trovo che nonostante il debug sia impostato su True, non ottengo i messaggi di errore sullo schermo. – disruptive
Realmente mi sono reso conto che tutti i messaggi di errore sono stati inviati al terminale su cui stavo girando il mio server. Eppure la modalità di debug si è rivelata utile per altri test (e stampa esattamente dove si esegue il comando curl) – alisa