Skip to content

Latest commit

 

History

History
208 lines (128 loc) · 5.15 KB

homework-2.md

File metadata and controls

208 lines (128 loc) · 5.15 KB

Week 2 Homework 2: Test APIs with Postman

In this homework exercise, you will use Postman to make API calls to your FastAPI app.

Postman is an API client. It lets you send requests such as GET, POST, PATCH, and DELETE without writing code.

What You Will Learn

  • How to download Postman.
  • How to send a GET request.
  • How to send a POST request with JSON.
  • How to send a PATCH request.
  • How to send a DELETE request.
  • How to read the response status code and JSON body.

Part A: Download Postman

  1. Open the official Postman download page:
https://www.postman.com/downloads/

Use the official Postman site only.

  1. Download the desktop app for your operating system.

Postman supports Windows, macOS, and Linux. The official installation guide says to download the latest desktop app from the Postman download page and choose the option for your operating system.

For installation steps specific to your system, follow the official Postman installation guide.

Use the desktop app for this homework so you can send requests directly to your local API at 127.0.0.1.

  1. Install and open Postman.

You may be asked to create or sign in to a Postman account. Follow the screen instructions.

Watch the Basics First

Watch How to Send Your First API Request in Postman, published on Postman's official YouTube channel (about 9 minutes).

Focus on creating a request, sending a GET request, and reading the JSON response. The video demonstrates a different API; for this homework, use your Notes API and the URLs below.

The video uses an earlier Postman interface, so some buttons may look different. The current official quick-start guide provides an up-to-date reference.

Part B: Start Your API

  1. Open your week2-notes-crud project in VS Code.

  2. Activate your virtual environment.

  3. Run the API:

uvicorn main:app --reload

Keep the server running while you use Postman. If the API is already running, skip the second start command. An Address already in use error means another process is using port 8000.

Part C: Send a GET Request

  1. In Postman, open a new HTTP request tab using Add (the + button) in the workbench.

  2. Set the method to:

GET
  1. Enter this URL:
http://127.0.0.1:8000/notes
  1. Click Send.

Expect status 200 and a JSON list. If there are no notes yet, you may see:

[]

Part D: Send a POST Request

  1. Create a new request.

  2. Set the method to:

POST
  1. Enter this URL:
http://127.0.0.1:8000/notes
  1. Open the Body tab.

  2. Select raw.

  3. Change the body type to JSON.

  4. Add this JSON:

{
  "title": "Postman note",
  "text": "I created this note using Postman."
}
  1. Click Send.

Expect status 201 and a response with an id, title, and text. Copy the returned id for the next requests. IDs do not restart at 1 when notes are deleted, so do not assume your new note has ID 1.

Part E: Send a PATCH Request

  1. Create a new request.

  2. Set the method to:

PATCH
  1. Enter this URL:
http://127.0.0.1:8000/notes/1

Replace 1 with the exact id returned by your POST request.

  1. Open the Body tab.

  2. Select raw and JSON.

  3. Add this JSON:

{
  "title": "Updated from Postman"
}
  1. Click Send.

Expect status 200 and the updated note. The original text should remain unchanged because you sent only a title.

Part F: Send a DELETE Request

  1. Create a new request.

  2. Set the method to:

DELETE
  1. Enter this URL:
http://127.0.0.1:8000/notes/1

Replace 1 with the exact id returned by your POST request.

  1. Click Send.

Expect status 200 and:

{"message":"Note deleted"}
  1. Send GET /notes again to check that this note is gone. Other notes may still be listed. Also request the deleted ID with GET /notes/{note_id}; expect 404 and {"detail":"Note not found"}.

Part G: Stop the Server

  1. Go back to the VS Code terminal.

  2. Stop the server with:

Ctrl + C

Troubleshooting

  • Connection refused / could not send request: check that Uvicorn is running and the URL uses http://127.0.0.1:8000.
  • 422 after POST: check that the body is set to raw JSON and contains both title and text as strings.
  • 404 after PATCH or DELETE: use the ID from your POST response and check that the note has not already been deleted.

Submission Checklist

Before you finish, make sure:

  • Postman is installed.
  • GET /notes works in Postman.
  • POST /notes creates a note.
  • PATCH /notes/{note_id} updates a note.
  • DELETE /notes/{note_id} deletes a note.
  • You can find the response status code in Postman.

Homework 2 is complete. Follow the homework submission instructions to share your public repository in Teams for feedback, then complete the Weekend Activity: Cloud and AI.