Loading video player…

Consuming APIs - GET and POST

00:00 REST and Python: Consuming APIs In the next two parts of the course, you’ll see how to consume APIs with Python. To write code that interacts with REST APIs, most Python developers turn to requests to send HTTP requests.

00:16 This library abstracts away the complexities of making HTTP requests. It’s one of the few projects worth treating as if it’s part of the standard library.

00:26 To start using requests, you’ll need to install it first. You can use pip to install it and it’s good practice to do this into a virtual environment so on screen you’ll see the commands you’ll need to do this on macOS or Linux,

00:55 and here’s what you’d need to do the same on Windows. Note that only the activation line is different.

01:16 Now that you’ve got requests installed, you can start sending HTTP requests.

01:22 GET is one of the most common HTTP methods you’ll use when working with REST APIs. This method allows you to retrieve resources from a given API. GET is a read-only operation, so you shouldn’t try to use it to modify an existing resource.

01:38 To test out GET and the other methods in this section, you’ll use a service called {JSON}Placeholder. This free service provides fake API endpoints that send back responses that requests can process.

01:50 To try this out, start up the REPL and run the command seen on screen to send a GET request to a jsonplaceholder endpoint.

02:12 This code calls requests.get() to send a GET request to /todos/1, which responds with the to-do item with the ID of 1.

02:20 Then you call the .json() method on the response object to view the data that came back from the API.

02:29 The response data is formatted as JSON, a key-value store similar to a Python dictionary. It’s a very popular data format and the de facto interchange format for most REST APIs.

02:42 Beyond viewing the JSON data from the API, you can also view other things about the response. Here, you access response.status_code to see the HTTP status code.

02:53 You can also view the response’s HTTP headers with response.headers. This dictionary contains metadata about the response such as the content type of the response.

03:06 POST is another common HTTP method. As the name suggests, it allows the client to send a resource to the API to be processed. This method is commonly used in the processing of data sent by forms on web pages, and it allows the creation of new resources on the server.

03:24 You’ll now look at how to use requests to post data to a REST API to create a new resource. You’ll use {JSON}Placeholder again, but this time you’ll include JSON data.

03:35 In the request on screen, you can see the data that you’ll be sending. This JSON data contains information for a new to-do item. Back in the REPL, run the code seen on screen to create the to-do.

03:58 First, you create a dictionary containing the data for your to-do.

04:06 Then you pass this dictionary to the json keyword argument of requests.post(). When you do this, requests.post() automatically sets the request’s HTTP header Content-Type to application/json.

04:21 It also serializes todo into a JSON string, which it appends to the body of the request.

04:34 If you don’t use the json keyword argument to supply the JSON data, then you need to set Content-Type accordingly and serialize the JSON manually.

04:45 Here’s an equivalent version to the previous code.

05:07 In this code, you add a headers dictionary that contains a single header Content-Type set to application/json. This tells the REST API that you are sending JSON data with the request.

05:19 You then call requests.post(), but instead of passing todo to the json argument, you first call json.dumps(todo) to serialize it.

05:28 After it’s serialized, you pass it to the data keyword argument. The data argument tells requests what data to include in the request.

05:36 You also pass the headers dictionary to requests.post() to set the HTTP headers manually. When you call requests.post() like this, it has the same effect as the previous code, but gives you more control over the request.

05:53 Note that json.dumps() comes from the json package in the standard library. This package provides useful methods for working with JSON in Python and you can learn more about it in this Real Python video course.

06:07 Once the API responds, you call response.json() to view the JSON. The JSON includes a generated ID for the new to-do.

06:17 The 201 status code tells you that a new resource was created. Now you’ve seen what are arguably the most common request types. In the next section of the course, you’ll look at three others: PUT, PATCH, and DELETE.

Become a Member to join the conversation.