simple python requests example

Simple Python Requests Example

Python requests is a powerful library that allows developers to send HTTP requests easily. It can be used to send GET, POST, PUT, DELETE, and other requests. In this post, we will learn how to use the requests library to send a simple HTTP request.

Installation

Before we dive into the requests library, we need to install it. To install the requests library, you can use pip. Open up your terminal or command prompt and type the following command:

pip install requests

Sending a GET request

Let's start off by sending a simple GET request to a website. In this example, we will send a request to "https://www.google.com".

import requests

response = requests.get("https://www.google.com")

print(response.text)

In the code above, we imported the requests library and sent a GET request to "https://www.google.com". We then printed the response text which displays the HTML content of the website.

Sending a POST request

A POST request is used to submit data to a server to create or update a resource. To send a POST request, we can use the requests.post() method. Let's create a simple example where we send a POST request to a server with some data.

import requests

data = {
  "name": "John",
  "age": 30
}

response = requests.post("http://example.com/api/users", data=data)

print(response.status_code)

In the code above, we created a dictionary with some data and sent a POST request to "http://example.com/api/users" with the data. We then printed the response status code which displays the HTTP status code of the response.

Sending a PUT request

A PUT request is used to update an existing resource on the server. To send a PUT request, we can use the requests.put() method. Let's create a simple example where we send a PUT request to a server to update an existing user.

import requests

data = {
  "name": "John",
  "age": 35
}

response = requests.put("http://example.com/api/users/1", data=data)

print(response.status_code)

In the code above, we created a dictionary with some updated data for an existing user and sent a PUT request to "http://example.com/api/users/1" with the updated data. We then printed the response status code which displays the HTTP status code of the response.

Sending a DELETE request

A DELETE request is used to delete an existing resource on the server. To send a DELETE request, we can use the requests.delete() method. Let's create a simple example where we send a DELETE request to a server to delete an existing user.

import requests

response = requests.delete("http://example.com/api/users/1")

print(response.status_code)

In the code above, we sent a DELETE request to "http://example.com/api/users/1" to delete an existing user. We then printed the response status code which displays the HTTP status code of the response.

Conclusion

Python requests is a powerful library that allows developers to send HTTP requests easily. We covered how to send GET, POST, PUT, and DELETE requests in this post. There are many more features of the requests library such as sending headers, handling cookies, and authentication. I encourage you to explore the documentation to learn more about the capabilities of the requests library.