I need to read the following request:
curl -X POST http://localhost:8080/cat
-d "{"name": "Tihon", "color": "red & white", "tail_length": 15, "whiskers_length": 12}"
how can i make it using flask ?
I tried:
from flask import Flask, request, jsonify
app = Flask(name)
@app.route('/cat', methods=['POST'])
def json_example():
request_data = request.get_json()
name = request_data.get('name')
color = request_data.get('color')
tail_length = request_data.get('tail_length')
whiskers_length = request_data.get('whiskers_length')
response_data = {
'name': name,
'color': color,
'tail_length': tail_length,
'whiskers_length': whiskers_length
}
return jsonify(response_data), 200
if name == 'main':
app.run(host='0.0.0.0', port=8080, debug=True)
but it returns an error 415. Also i tried with form.get() but it returns null
from flask import Flask, request, jsonify
app = Flask(name)
@app.route('/cat', methods=['POST'])
def add_cat():
# Получаем JSON-данные из тела запроса
name = request.form.get('name')
color = request.form.get('color')
tail_length = request.form.get('tail_length')
whiskers_length = request.form.get('whiskers_length')
return jsonify({'name': name, 'color': color, 'tail_length': tail_length,
'whiskers_length': whiskers_length}), 200
if name == 'main':
app.run(host='0.0.0.0', port=8080)