Module 03 / Project 03 — POST and Auth¶
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
- Sending POST requests with
requests.post() - Sending a JSON body with the
jsonparameter - Setting custom HTTP headers
- Understanding the difference between GET and POST
Why this project exists¶
Reading data is only half the story. Most APIs also let you create, update, and delete resources. This project teaches you how to send data to an API and how to attach headers that identify your client. JSONPlaceholder accepts POST requests and returns what the server would have created, so you can practice without any risk.
Run¶
Expected output¶
--- POST: Creating a new post ---
Status code: 201
Server response:
{
"title": "My First API Post",
"body": "This post was created by a Python script using requests.",
"userId": 1,
"id": 101
}
The server assigned ID: 101
--- GET with custom headers ---
Status code: 200
Request headers we sent:
User-Agent: learn-python-module03/1.0
Accept: application/json
Post title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
--- GET vs POST comparison ---
GET /posts/1 -> status 200, method retrieves existing data
POST /posts -> status 201, method creates new data
Key difference: GET reads, POST writes.
Alter it¶
- Change the POST body to include your own title and body text. Verify the server echoes back what you sent.
- Add a custom header called
X-Request-Sourcewith the value"python-learner". Print the request headers to confirm it was sent. - Try sending a POST with an empty body (
json={}). What does the server return?
Break it¶
- Use
requests.post()but pass the data asdata={"title": "test"}instead ofjson={"title": "test"}. What happens to the Content-Type header? Does the response change? - Send a POST to
/posts/1(an existing resource URL). What status code do you get? - Remove the
jsonparameter entirely from the POST call. What does the server return?
Fix it¶
- When using
data=instead ofjson=, manually set theContent-Typeheader toapplication/jsonand usejson.dumps()on the body. Verify the server responds the same as before. - Add a check after the POST: if
status_codeis not 201, print a warning with the actual status code. - After fixing, run both the correct and broken versions to confirm the difference.
Explain it¶
- What is the difference between the
json=parameter and thedata=parameter inrequests.post()? - What does HTTP status code 201 mean, and how does it differ from 200?
- Why would you set a custom
User-Agentheader when making API requests? - JSONPlaceholder returns
id: 101for every POST. Why is that, and how would a real API differ?
Mastery check¶
You can move on when you can:
- send a POST request with a JSON body from memory,
- explain the difference between GET and POST,
- set custom headers on a request,
- describe what
json=does compared todata=.
Related Concepts¶
Next¶
Continue to Error Handling.