File size: 792 Bytes
2fe069a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | from flask import Flask, request, jsonify
app = Flask(__name__)
# Define the addition function
def add_numbers(a, b):
return a + b
# Create an API route
@app.route('/add', methods=['POST'])
def add():
data = request.get_json() # get JSON data from the request
a = data.get('a')
b = data.get('b')
# Check if both a and b are provided
if a is None or b is None:
return jsonify({"error": "Please provide both 'a' and 'b' numbers."}), 400
try:
a = float(a)
b = float(b)
except ValueError:
return jsonify({"error": "Inputs must be numbers."}), 400
result = add_numbers(a, b)
return jsonify({"sum": result})
# Run the app
if __name__ == '__main__':
app.run(debug=True)
|