Код: Выделить всё
`#%% ALL server logic
from flask import Flask,jsonify,request,make_response
app = Flask(__name__)
@app.route('/')
def nakedRoute():
print('inside the teapot')
return "I'm a teapot", 418
@app.route('/echo/', methods=['POST'])
def serverAPI_ejemplo():
try:
if not request.is_json:
return make_response(jsonify({"error": "I was expecting a JSON"}), 400)
data = request.json['key'] + 1
return jsonify({"key":data}), 200 # Código de estado 200 (OK)
except Exception as e:
return make_response(jsonify({"error": 33}), 500)
#%% Mount the server
if __name__ == "__main__":
app.run(port=5000, debug=True)
если захочу чтобы пройти первый маршрут
Код: Выделить всё
# %% TEST naked router
with app.app_context():
result= nakedRoute()
print(result)
Код: Выделить всё
# %% TEST /echo/ for error 500
with app.app_context():
result= serverAPI_ejemplo()
print(result)
кроме случаев, когда я собираюсь добавить Json в CALL
Код: Выделить всё
# %% TEST /echo/ for a JSON
with app.app_context():
my_JSON = {"key":33}
#...???
result= serverAPI_ejemplo()
print(result)`
Подробнее здесь: https://stackoverflow.com/questions/786 ... code-cells