

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
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 fetch(url, {
method: "PUT",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(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 updateUser() and getUserById() functions. They should update and retrieve individual user resources respectively. For each fetch request:
method to match the CRUD action being performedContent-Type and X-API-Key headersmode to cors.json() method of the Response objectThe updateUser function will also have to encode the user data in the body of the request using JSON.stringify.
We've included the fullURL creation logic for you in both functions, we'll be talking more about URL building in the next chapter.