Simple Web Service for Basic Arithmetic Operations

  • Share this:

Code introduction


This function creates a simple web service that can receive two numbers and return their sum or difference via HTTP GET requests.


Technology Stack : Bottle

Code Type : Web API

Code Difficulty : Intermediate


                
                    
from bottle import route, run, request, response

def random_bottle_function():
    @route('/add/<int:num1>/<int:num2>')
    def add(num1, num2):
        result = num1 + num2
        response.content_type = 'application/json'
        return {"sum": result}

    @route('/subtract/<int:num1>/<int:num2>')
    def subtract(num1, num2):
        result = num1 - num2
        response.content_type = 'application/json'
        return {"difference": result}

    run(host='localhost', port=8080)

random_bottle_function()                
              
Tags: