Skip to content

Latest commit

 

History

History
170 lines (122 loc) · 5.54 KB

03-api.en.md

File metadata and controls

170 lines (122 loc) · 5.54 KB

STEP3: Make a listing API

1. Call an API

📖 Reference

Install curl

Check if you can see command not found error on your console. You need to install curl. This how to install curl be used on macOS and Linux and Windows Subsystem for Linux (WSL).

1. How to install curl

$ brew install curl

2. Check the curl version

$ curl --version

GET request

In step2, you ran a service on your local server where you accessed the endpoint from http://127.0.0.1:9000 using your browser. Use the following curl command to access this endpoint. Please open a new terminal and enter the following command. Install curl if necessary.

curl -X GET 'http://127.0.0.1:9000'

Check if you can see {"message": "Hello, world!"} on your console.

POST request.

In the example implementation, you can see /items endpoint. Use curl to call this endpoints.

$ curl -X POST 'http://127.0.0.1:9000/items'

This endpoint expects to return {"message": "item received: <name>"}, but you should be seeing something different.

Modify the command as follows and see that you receive {"message": "item received: jacket"}. Investigate what causes the differences.

$ curl -X POST \
  --url 'http://localhost:9000/items' \
  -d name=jacket

🔰 Points

  • Understand the difference between GET and POST requests.
  • Why do we not see {"message": "item received: <name>"} on accessing http://127.0.0.1:9000/items from your browser?
    • What is the HTTP Status Code when you receive these responses?
    • What do different types of status code mean?

2. List a new item

Make an endpoint to add a new item

📖 Reference

The endpoint already implemented (POST /items) takes name as an argument. Modify the API such that it also accepts category information.

  • name: Name of the item (string)
  • category: Category of the item (string)

Since the information cannot be retained with the current implementation, save this into a JSON file. Make a file called items.json and add new items under items key.

items.json is expected to look like the following.

{"items": [{"name": "jacket", "category": "fashion"}, ...]}

3. Get a list of items

Implement a GET endpoint /items that returns the list of all items. The response should look like the following.

# Add a new item
$ curl -X POST \
  --url 'http://localhost:9000/items' \
  -d 'name=jacket' \
  -d 'category=fashion'
# Expected response for /items endpoint with POST request
{"message": "item received: jacket"}
# Get a list of items
$ curl -X GET 'http://127.0.0.1:9000/items'
# Expected response for /items endpoint with GET request
{"items": [{"name": "jacket", "category": "fashion"}, ...]}

4. Add an image to an item

Change the endpoints GET /items and POST /items such that items can have images while listing.

  • Make a directory called images
  • Hash the image using sha256, and save it with the name <hash>.jpg
  • Modify items such that the image file can be saved as a string
# POST the jpg file
curl -X POST \
  --url 'http://localhost:9000/items' \
  -F 'name=jacket' \
  -F 'category=fashion' \
  -F 'image=@images/local_image.jpg'
{"items": [{"name": "jacket", "category": "fashion", "image_name": "510824dfd4caed183a7a7cc2be80f24a5f5048e15b3b5338556d5bbd3f7bc267.jpg"}, ...]}

🔰 Point

  • What is hashing?
  • What other hashing functions are out there except for sha256?

5. Return item details

Make an endpoint GET /items/<item_id> to return item details.

$ curl -X GET 'http://127.0.0.1:9000/items/1'
{"name": "jacket", "category": "fashion", "image": "..."}

6. (Optional) Understand Loggers

Open http://127.0.0.1:9000/image/no_image.jpg on your browser. This returns an image called no image but the debug log is not displayed on your console.

Image not found: <image path>

Investigate the reason why this is the case. What changes should be made to display this message?

🔰 Points

  • What is log level?
  • On a web server, what log levels should be displayed in a production environment?

🔰 Points

Check if you understand the following concepts.

  • port number
  • localhost, 127.0.0.1
  • HTTP request methods (GET, POST...)
  • HTTP Status Code (What does each of 1XX, 2XX, 3XX, 4XX, 5XX mean?)

Next

STEP4: Database