How it works...

Two kinds of resources are typically accessed. Single resources (https://jsonplaceholder.typicode.com/posts/X) and collections (https://jsonplaceholder.typicode.com/posts):

  • Collections accept GET to retrieve them all and POST to create a new resource
  • Single elements accept GET to get the element, PUT and PATCH to edit, and DELETE to remove them

All the available HTTP methods can be called in requests. In the previous recipes, we used .get(), but .post(), .patch(), .put(), and .delete() are available.

The returned response object has a .json() method that decodes the result from JSON. 

Equally, to send information, a json argument is available. This encodes a dictionary into JSON and sends it to the server. The data needs to follow the format of the resource or an error may be raised.

GET and DELETE don't require data, while PATCH, PUT, and POST do require data.

The referred resource will be returned, and its URL is available in the header location. This is useful when creating a new resource, where its URL is not known beforehand.

The difference between PATCH and PUT is that the latter replaces the whole resource, while the first does a partial update.