Server Side Calculator Using API
A simple calculator UI submits form to server to calculate the result.
This is a simple API which can also be used by the amended Simple Calculator.
You may want to use an API tool for testing the API.
We have provided both a Swagger UI and a redoc UI here to access the API but these may not provide the full flexibility you need to test an API.
The API has an Open API file
The API can be found at:
/apps/api/calculator/There are two endpoints:
calculate
sequence
The calculate end point:
/apps/api/calculator/calculateAccepts POST and GET requests.
These can be either application/json or application/x-www-form-urlencoded
If using the application/x-www-form-urlencoded content then the parameters are the same as those used by the Simple Calculator application:
number1number2functionIf using the JSON format then use the following format:
{
"operation": "plus",
"left": "1",
"right": "2"
}
There is also an additional input format, instead of supplying a left and right value, supply a list of operands:
{
"operation": "plus",
"operands": [ "1", "2", "3"]
}
For a GET request, the values need to be encoded into the request as query params:
e.g. .../calculate?operation=plus&left=1&right=2
Params:
operation e.g. plus, minus, etc.left e.g. 1right e.g. 2operands e.g. [1, 2]The above would add 1 and 2 and respond with:
{
"type": "VALUE",
"value": "3"
}
Any errors result in an error response:
{
"type": "INPUT_ERROR",
"value": "Unrecognised Operation: add valid values are (plus, times, minus, divide)"
}
The sequence end point:
/apps/api/calculator/sequenceAccepts POST requests.
These can be application/json
To calculate (4 - 1) * 3:
{"operations": [
{
"operation": "plus",
"right": 4
},
{
"operation": "minus",
"right": "1"
},
{
"operation": "times",
"right": 3
}
]}
would result in:
{
"type": "VALUE",
"value": "9"
}
A simple calculator UI submits form to server to calculate the result.
A simple calculator API
A simple calculator API with the UI rendered using Redoc