This task is to create a simple server in Python that handles GET
, POST
, and DELETE
request methods. These methods are the basics of any CRUD application. To see a working example of this task follow the setup below.
You will create a CRUD application in Python using Flask, an easy way to handle HTTP commands. You will interface and perform reads, deletes, and updates with a dummy database in the form of a database.json
file. Below are the following requests and their expected responses. All requests will be to the /
or “root” endpoint.
These steps should point you in the right direction to get your environment setup.
cd
into it.
$ git clone https://github.com/jonathanlo411/temp.git
Cloning into 'temp'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
$ cd temp
venv
and activate it.python3 -m venv venv
source venv/Scripts/activate
// Sometimes setup is different
source venv/bin/activate
pip install Flask
server.py
.# Imports
from flask import Flask, request
from json import load, dump
# Init Flask app
app = Flask(__name__)
flask --app <your_py_file.py> --debug run
GET
The GET
method will query the database and return all items. Authentication is not required.
// Response
{
"data": [
{
"author": "Ray Bradbury",
"score": 7.1,
"title": "Fahrenheit 451"
},
{
"author": "Homer",
"score": 3.2,
"title": "Iliad"
},
{
"author": "William Shakespeare",
"score": 5.8,
"title": "Macbeth"
},
{
"author": "William Golding",
"score": 5.1,
"title": "Lord of the Flies"
},
{
"author": "Aldous Huxley",
"score": 6.3,
"title": "Brave New World"
}
],
"message": "GET Request Recieved",
"success": 1
}
POST
The POST
method will add an additional item to the database. Authentication is required in the form of a query parameter of key
. The value should be set to sample_key
and if it is not, should return an error. The JSON data of the additional item to be added will be put into the body (see example below). In addition, the Content-Type
in the headers should be set to application/json
.
key
: sample_key
Content-Type
: application/json
raw
: see below
// Body Request
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"score": 9.2
}
// Response
{
"data": [
{
"author": "Ray Bradbury",
"score": 7.1,
"title": "Fahrenheit 451"
},
{
"author": "Homer",
"score": 3.2,
"title": "Iliad"
},
{
"author": "William Shakespeare",
"score": 5.8,
"title": "Macbeth"
},
{
"author": "William Golding",
"score": 5.1,
"title": "Lord of the Flies"
},
{
"author": "Aldous Huxley",
"score": 6.3,
"title": "Brave New World"
},
{
"author": "Harper Lee",
"score": 9.2,
"title": "To Kill a Mockingbird"
}
],
"message": "POST Request Recieved",
"success": 1
}
DELETE
The DELETE
method will remove all entries of books with the provided title. Authentication is required in the form of a query parameter of key
. The value should be set to sample_key
and if it is not, should return an error. An additional query parameter of title
should be present in the request. The value will be set to the target of deletion.
key
: sample_key
title
: title of target deletion (Brave New World
)// Response
{
"data": [
{
"author": "Ray Bradbury",
"score": 7.1,
"title": "Fahrenheit 451"
},
{
"author": "Homer",
"score": 3.2,
"title": "Iliad"
},
{
"author": "William Shakespeare",
"score": 5.8,
"title": "Macbeth"
},
{
"author": "William Golding",
"score": 5.1,
"title": "Lord of the Flies"
},
{
"author": "Aldous Huxley",
"score": 6.3,
"title": "Brave New World"
}
],
"message": "DELETE Request Recieved",
"success": 1
}
If any of the requests fail, there should be a JSON response as show below with the status code of 403.
// Response
{
"success": 0,
"message": "<method> Unsuccessful"
}
*Note: This is a completed version of the task and should only be used to make comparisons when confused on the task. You can also use it to test various responses against it. The example, however, is NOT something your should copy from.
cd
into it.
git clone https://github.com/ucsdds3/starter-tasks-backend-2
cd starter-tasks-backend-2
requirements.txt
.
python3 -m venv venv
source venv/Scripts/activate
pip install -r requirements.txt
flask --app server run