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.

Avatar image for Brij

Brij on Feb. 14, 2025

incomplete commands given. could not complete this exercise

---------step-1------- (snwprk) PS C:\snowpark\snwprk\Scripts> python -m bpython -q C:\snowpark\snwprk\Scripts\python.exe: No module named bpython

-------------step-2------

(snwprk) PS C:\snowpark\snwprk\Scripts> pip install bpython … … Installing collected packages: wcwidth, pyxdg, ansicon, pygments, jinxed, greenlet, cwcwidth, blessed, curtsies, bpython Successfully installed ansicon-1.89.0 blessed-1.20.0 bpython-0.24 curtsies-0.4.2 cwcwidth-0.1.9 greenlet-3.1.1 jinxed-1.3.0 pygments-2.19.1 pyxdg-0.28 wcwidth-0.2.13

-------------------tried–1st-cmd-got-error-blw-----

(snwprk) PS C:\snowpark\snwprk\Scripts> python -m bpython -q Traceback (most recent call last): File “C:\Python\Python38\lib\runpy.py”, line 194, in _run_module_as_main return _run_code(code, main_globals, None, File “C:\Python\Python38\lib\runpy.py”, line 87, in _run_code exec(code, run_globals) File “C:\snowpark\snwprk\lib\site-packages\bpython__main__.py”, line 27, in <module> from .curtsies import main File “C:\snowpark\snwprk\lib\site-packages\bpython\curtsies.py”, line 10, in <module> import curtsies File “C:\snowpark\snwprk\lib\site-packages\curtsies__init__.py”, line 4, in <module> from .window import FullscreenWindow, CursorAwareWindow File “C:\snowpark\snwprk\lib\site-packages\curtsies\window.py”, line 30, in <module> from .termhelpers import Cbreak File “C:\snowpark\snwprk\lib\site-packages\curtsies\termhelpers.py”, line 1, in <module> import tty File “C:\Python\Python38\lib\tty.py”, line 5, in <module> from termios import * ModuleNotFoundError: No module named ‘termios’

------------------step-4–

(snwprk) PS C:\snowpark\snwprk\Scripts> pip install termios ERROR: Could not find a version that satisfies the requirement termios (from versions: none) ERROR: No matching distribution found for termios (snwprk) PS C:\snowpark\snwprk\Scripts>

Avatar image for Phil

Phil on Feb. 14, 2025

Hi @Brij 👋🏻

If you watch the first video again, you’ll see that Darren mentions he uses bpython in this course.

This is purely optional, however bpython unfortunately isn’t supported on Windows straight out of the box.

If you look at this Real Python tutorial – Discover bpython: A Python REPL With IDE-Like Features – you’ll see that it suggests installing Windows Subsystem for Linux (WSL) and then installing bypthon from inside that environment.

Hope that helps 😀

Avatar image for Brij

Brij on Feb. 14, 2025

1) in 1st video he did mentioned Bpython is option and any cmds he is showing will work on repl. 2) at 1:50 Darren is showing how to execute GET command in windows environment. But that did not work 3) if you see log in step 2, I was able to install bpython successfully 4) rather error is this “ModuleNotFoundError: No module named ‘termios’ ” 5) I tried to install ‘terminos’ module, but it failed. 6) So in total it is incomplete course for windows user

Avatar image for Phil

Phil on Feb. 14, 2025

Hi again @Brij

Like I said previously, bpython is not directly supported on Windows.

Even though you may have installed bpython, it will not work as it relies on modules such as termios which are only available on POSIX systems like Linux and macOS.

Instead of typing python -m bpython -q like Darren does in the video, just type python to start up the standard REPL, and you should be good to go.

Avatar image for Brij

Brij on Feb. 15, 2025

Thanks a ton @Phil. it working now

Become a Member to join the conversation.