实现
python3实现简单示例,具体开发需结合项目需求
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
@app.route('/api', methods=['POST'])
def api():
if request.method == 'POST':
data = request.get_data()
data = json.loads(data)
message = data['message']
print(message)
# 两种返回方式都可以
# return {'response': f'Received message: {message}'}
return jsonify({'response': 'Message received:'+message})
if __name__ == '__main__':
app.run(host='192.168.11.74', port=5000)
测试
使用postman 向192.168.11.74:5000/api发送以下json请求
{
"message": "Hello, world!"
}
收到以下响应
{
"response": "Received message: Hello, world!"
}