

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: HTTP Methods - GET
incomplete
2: Why Use HTTP Methods?
incomplete
3: POST Requests
incomplete
4: Status Codes
incomplete
5: Status Code Property
incomplete
6: PUT
incomplete
7: PATCH vs. PUT
incomplete
8: Delete
incomplete
This lesson's interactive features are locked, please to keep using them
The HTTP PUT method creates or more commonly, updates a representation of the target resource with the contents of the request's body. In short, it updates a resource's properties.
await pyfetch(
url,
method="PUT",
headers={
"Content-Type": "application/json",
},
body=json.dumps(data),
)
While POST and PUT are both used for creating resources, PUT is more common for updates and is idempotent, meaning it's safe to make the request multiple times without changing the server state. For example:
POST /users/bob (create bob user)
POST /users/bob (create duplicate bob user)
POST /users/bob (create duplicate bob user)
PUT /users/bob (create bob user if it doesn't exist)
PUT /users/bob (update bob user with the same data)
PUT /users/bob (update bob user with the same data)
Complete the update_user(base_url: str, user_id: str, data: User, api_key: str) -> User and get_user_by_id(base_url: str, user_id: str, api_key: str) -> User functions. They should update and retrieve individual user resources respectively. For each pyfetch request:
method to match the CRUD action being performedContent-Type and X-API-Key headersawait response.json() methodThe update_user function will also have to encode the user data in the body of the request using json.dumps().
We've included the full_url creation logic for you in both functions, we'll be talking more about URL building in the next chapter.