flask post

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/api/post_example', methods=['POST'])
def post_example():
    if request.method == 'POST':
        try:
            data = request.get_json()  # Retrieve JSON data from the request
            if 'message' in data:
                message = data['message']
                # Perform necessary operations with the received data (e.g., save to database)
                # Replace the following line with your specific logic
                response = {'status': 'success', 'message': f'Received message: {message}'}
                return jsonify(response), 200  # Return a JSON response with status code 200
            else:
                response = {'status': 'error', 'message': 'Missing "message" in the request JSON'}
                return jsonify(response), 400  # Return an error response with status code 400 for missing data
        except Exception as e:
            response = {'status': 'error', 'message': f'Error processing the request: {str(e)}'}
            return jsonify(response), 500  # Return an error response with status code 500 for general errors
    else:
        response = {'status': 'error', 'message': 'Invalid request method'}
        return jsonify(response), 405  # Return an error response with status code 405 for invalid request method

if __name__ == '__main__':
    app.run(debug=True)