You can download this code by clicking the button below.
This code is now available for download.
This function defines a simple web API that accepts POST requests in JSON format, containing two numbers, and returns their sum.
Technology Stack : Bottle
Code Type : Web API
Code Difficulty : Intermediate
from bottle import route, run, request, response
def xxx(arg1, arg2):
@route('/add', method='POST')
def add():
num1 = request.json.get('num1')
num2 = request.json.get('num2')
if num1 is None or num2 is None:
response.status = 400
return "Missing 'num1' or 'num2' in JSON body"
result = num1 + num2
response.headers['Content-Type'] = 'application/json'
return {'result': result}
# Running the Bottle web server
run(host='localhost', port=8080)