We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

HTTP PUT

The HTTP PUT method creates or more commonly, updates the target resource with the contents of the request's body. Unlike GET and POST, there is no http.Put function. You will have to create a raw http.Request that an http.Client can Do.

POST vs. PUT

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 same request multiple times without changing the server state more than once. 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)

Assignment

Complete the updateUser and getUserById functions.

updateUser

The updateUser function takes a baseURL, id and apiKey string, and data User as input. It returns a User and error. It should:

getUserById

getUserById takes a baseURL, id and apiKey string. It returns a User and error. It should:

We've included the fullURL creation logic for you in both functions, we'll be talking more about URL building in the next chapter.