You can download this code by clicking the button below.
This code is now available for download.
This code creates a Flask web application that can accept JSON data containing two numbers and return their sum.
Technology Stack : Flask, JSON
Code Type : Flask Web Application
Code Difficulty : Intermediate
from flask import Flask, jsonify, request
def create_app():
app = Flask(__name__)
@app.route('/add', methods=['POST'])
def add_numbers():
data = request.get_json()
a = data.get('a')
b = data.get('b')
if a is None or b is None:
return jsonify({"error": "Missing 'a' or 'b' in JSON payload"}), 400
return jsonify({"result": a + b})
return app
# Flask is used to create a web application that can accept JSON data and perform basic arithmetic operations.