Description: create an API server that returns Dad Jokes as JSON
Language: Python
Project type: School project, solo
Purpose: Learn how to create an API web application using Python's flask modules
Run the file "dadjokes.py", this runs the flaskapp in your localhost
Next, run the localhost in your browser using "http://localhost:3001/". Further instructions for routes are below.
This API application serves the following routes.
All routes return an application/json content type and appropriate HTTP status code.
/
Returns JSON {'success': True, 'message': 'This is the home page'} with a application/json content type and a 200 status header.
/random
Pulls a random joke out of dadjokes.json and send it back to the browser.
An example request may return the following JSON string.
{
"id": "b6dc0870",
"name": "Color Blind",
"joke": "Found out I was colour blind the other day... That one came right out the purple."
}Subsequent runs return different jokes, although the tester does account for the fact that random isn't always random.
/joke
This route allows you to request a specific joke from the data file by passing an id parameter. For example, if you want to get joke #123 the request would be /joke?id=123.
/joke?id=c9aaad4d returns the following JSON string.
{
"id": "c9aaad4d",
"name": "The Royal Flush Of The Jungle",
"joke": "Why shouldn't you play cards in the jungle? Too many cheetahs."
}Server also checks that the id parameter is there. If it is not, the server returns a 404 status header.
If the requested joke id is not in the data, the server sends a 404 status header.
dadjokes.json came from GitHub