Power a UI with your API

In this lab you will be tasked to create three endpoints:

1) An endpoint “/add” that accepts GET requests and adds two numbers: (num1 and num2)

2) An endpoint “/sub” that accepts POST requests of application/x-www-form-urlencoded content-type that subtracts two numbers (num1 and num2)

3) An endpoint “/mult”) that accepts POST requests of application/json content type that multiplies two numbers (num1 and num2)

For all three endpoints you should send a response of application/json format with the format:

{ “answer”: [the answer] }

Web Client!

I have provided a web client that will function if you run your server: Please find the files here.

Please download the entire folder, unzip it if necessary, and then double click on calc.html to load the web calculator. The web page will send HTTP requests to your local server to do the calculation.

To get your webserver working with the web calculator, be sure to add this code to the end of .your server code:

def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
    return response

Without the above code the web calculator will not work due to CORS – Cross Origin Resource Sharing policy. By default there are protections that your browser has to ensure that your web browser does not send requests (via AJAX) to another domain (localhost). The above code lifts that restriction.

If your web calculator works when you run your server with the added above code, you have completed the lab.