Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Other HTTP Methods

This lesson covers three other HTTP methods than the GET method you learned earlier about. These include:

  • POST
  • PUT
  • DELETE

To test these methods the httpbin.org service from Kenneth Reitz is used. Besides the three HTTP methods covered in this video a few other popular exist including HEAD, PATCH, and OPTIONS.

00:00 So, beyond the GET request, there are other HTTP methods. requests provides methods for each of these—POST, PUT, DELETE, HEAD, PATCH, OPTIONS.

00:10 We’ll be testing these using Kenneth Reitz’s httpbin service, and that’s found at httpbin.org and then slash (/) whatever the name of the method is—could be post, put, delete, and so forth. So let’s test it out. Okay, for this we’re going to really work mostly in the terminal, so I’m going to close our file and resize the window.

00:35 Okay, I’m back in my REPL. Let’s import requests and save our URL—at least the beginning of it—

00:47 and that’s where we’ll add all the other pieces. And as we do these, let’s go ahead and just save them into a Response, so we can kind of look at how the request goes.

00:57 requests has a similar signature to each of these HTTP methods. Let’s start with post() and put our url, and then we’ll just add on here, the string 'post'.

01:10 And then for post(), we would be sending some form of data into it as a dictionary. All right, let’s try it. And we can again look at our response, we can look directly at its .status_code, which is 200, success.

01:26 And we can look at the .headers, or we can look at a specific header.

01:37 Let’s try another one. Let’s try put().

01:50 It was a success. Let’s try one more. We’ll try delete(). This is just to give you an idea of what these requests look like. What’s nice is they do follow a very similar pattern.

02:05 And this one, where somebody’s just choosing to delete at that location. Okay, and we can look at status. On this one, we can actually do that thing where we change it into a JSON response to look at the data. Take our response, and use this method.

02:21 We could look at a variety of things, like we could look at here, what the arguments were, what the data was—just by putting in the key. So as you can see, headers, response bodies, status codes—they’re all returned in the Response for each of these methods. Next, we’re going to dive a little bit deeper into the POST, PUT, and PATCH methods and learn how they differ from the other request types.

Avatar image for Pygator

Pygator on Sept. 15, 2019

I still don’t understand why or what is the motivation for delete, put, post methods. Do you have other resources for them?

Become a Member to join the conversation.