Здраствуйте, пишу код не давно хотел сделать небольшое приложения с использованием запроса.
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)
Делаю запрос через curl:
curl -H -X POST "aplication/json" {"Name": "Sasha"} "http://127.0.0.1:5000/add"
Вывод:
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>
Подскажите в чем ошибка??
Переписал строчку как просили:
$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>
Ошибка на сервере:
'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 -
Поменял две строчки:
f = request.json()
return f
На:
f = request.get_json()
return "Add the json request."
Все заработало!)