You can download this code by clicking the button below.
This code is now available for download.
This function is part of a Flask web application. It defines a route that accepts POST requests, retrieves two numbers from the request's JSON, and returns their sum.
Technology Stack : Flask, Python
Code Type : Flask Web Application
Code Difficulty : Intermediate
from flask import Flask, request, jsonify
def create_app():
app = Flask(__name__)
@app.route('/add', methods=['POST'])
def add():
data = request.json
num1 = data.get('num1')
num2 = data.get('num2')
if num1 is None or num2 is None:
return jsonify({'error': 'Missing num1 or num2 in the request'}), 400
result = num1 + num2
return jsonify({'result': result})
return app
# Flask app to add two numbers