Random Quote Web API

  • Share this:

Code introduction


This function defines a simple web API. When a user accesses the '/quote' path, it returns a random quote.


Technology Stack : Bottle

Code Type : Web API

Code Difficulty : Intermediate


                
                    
from bottle import route, run, request, response

def get_random_quote():
    quotes = [
        "The best way to predict the future is to invent it.",
        "Innovation distinguishes between a leader and a follower.",
        "Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work. And the only way to do great work is to love what you do.",
        "Stay hungry, stay foolish."
    ]
    import random
    selected_quote = random.choice(quotes)
    return selected_quote

@route('/quote', method='GET')
def handle_quote():
    quote = get_random_quote()
    response.content_type = 'text/plain'
    return quote

# Running the Bottle server
if __name__ == '__main__':
    run(host='localhost', port=8080)                
              
Tags: