Hello, I am writing a code for a long time I wanted to make a small application using the request.
from flask import Flask, request import json app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' @app.route('/a') def a(): page = request.args.get('page', default = 0, type = int) filter = request.args.get('filter', default = '*', type = str) RE = 'page: ' + str(page) + '\n' + 'filter: ' + str(filter) return RE @app.route('/add', methods=['POST']) def add(): f = request.json() return f if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) I make a request through curl:
curl -H -X POST "aplication/json" {"Name": "Sasha"} "http://127.0.0.1:5000/add" Conclusion:
curl: (6) Could not resolve host: POST curl: (6) Could not resolve host: aplication curl: (3) [globbing] unmatched brace in column 1 curl: (3) [globbing] unmatched close brace/bracket in column 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>405 Method Not Allowed</title> <h1>Method Not Allowed</h1> <p>The method is not allowed for the requested URL.</p> Tell me what is the error ??
Rewrote the line as requested:
$curl -X POST -H "aplication/json" -d '{"Name": "Sasha"}' "http://127.0.0.1:5000/add" <!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> Server Error:
'The view function did not return a valid response. The' TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. 127.0.0.1 - - [06/Jul/2018 18:39:38] "POST /add HTTP/1.1" 500 - Changed two lines:
f = request.json() return f On:
f = request.get_json() return "Add the json request." It all worked!)