Enable Dark Mode!
getting-started-with-the-requests-library-in-python.jpg
By: Anjhana AK

Getting Started with the Requests Library in Python

Python is a popular programming language used for various purposes, including machine learning, web development, and software development. Python offers a broad range of libraries, and one of them is the requests library, which is commonly used to send HTTP requests to specified URLs. When we send a request using the library, we receive a response from the URL to which we sent the request.

$ pip install requests

We can install the request library by running this command in the terminal, and then use it in our Python file by importing the request library.

Import requests

There are some built-in methods available in the request library for sending HTTP requests.

  • Get
  • Post
  • Put
  • Delete
  • Head
  • Patch
  • Options

1. GET

It is the most commonly used method in the request library, used to retrieve data from a URL. We can make a GET request by

requests.get(‘url’)

There are some optional parameters like auth, params, and timeout

For example, auth is used for authentication purposes.

response = requests.get(
'https://panel.sendcloud.sc/api/v2/user/addresses/sender',
auth=(public_key, private_key))
print(response.status_code, “status”)
response.content
response.text
response.json()
response.headers

Here, we make a GET request to a URL, and we get a response from the server. and response.status_code will print the status of the request. When the request is successful, we will get ‘200 OK’.

There are some other status codes, including 204 No Content, 400 Bad Request, and 404 Not Found. From the server's response, we will obtain other useful information.

We can access this information in different formats using response methods.

  • Response.content - To get the data in bytes format
  • Response.text - To get the data in string format
  • Response.json() - To get the response data in json format. We can access the data since it is a dictionary.
  • Response.headers - Returns header data as a dictionary, and these dictionary keys are case insensitive.
  • Response.encoding - Returns the encoding used to decode response content.

When we send a request, we can use the query parameters to customize the request.

sender_address = “1234”
response = requests.get(
'https://panel.sendcloud.sc/api/v2/shipping_methods',
auth=(public_key, private_key),
params={'sender_address': sender_address}, headers={'key': 'value'})

Here, use a query parameter sender address and pass a default value to that. When we get the response data, the response will be modified using the sender address value.query parameters can be passed to a request as a dictionary, a list of tuples, or even bytes.

We can customize the header by using the header parameter, which helps to pass a dictionary of HTTP headers.

2. POST

Post request is the most commonly used HTTP method and sends a post request to a URL, which helps to send data to a server.

requests.post(url, data={key: value}, json={key: value})

When using a POST request, we typically utilize parameters such as data and JSON to send data to the server. Through the data parameter, we can pass the values as a dictionary, a list of tuples, bytes, or as file-like objects.

When the server expects data in JSON format, the json parameter can be used to send it.

response = requests.post(
'https://panel.sendcloud.sc/api/v2/parcels',
auth=(public_key, private_key),
json={"parcel":shipping_values})
response.request.url
response.request.body

Here, shipping values are a dictionary containing ID names and other information for creating a request on the SendCloud platform.

  • Response.request.url - Return the url that the request was sent to
  • Response.request.body - return the exact data that will be sent to the server

3. PUT

The put method is an HTTP method, and it is used for modifying an existing resource through a request; if the resource doesn’t exist, then it will create the resource. It is generally used for update operations, and the POST method for creating a resource. A PUT request is idempotent, which means repeating the same request multiple times will produce the same outcome.

Syntax:

requests.put(url, data={key: value})

response = requests.put(
'https://panel.sendcloud.sc/api/v2/parcels',
auth=(public_key, private_key),
data={"parcel"::{"id": 224458951,"name": "Mr Test",}})

Here, the parcel with the mentioned ID will be updated.

4. DELETE

The delete method is used to send a delete request to a URL, and it will delete the specified resource. The delete method returns a request.response object 200 is the status of successful response, and if there is a response describing status. If the action is pending, then the status code will be 202.

requests.delete(url, params={key: value}, args)

5. Head

The Request library head() method is used for sending head requests to a URL. The head method is used when we only need the status code or HTTP header instead of the content. The head method works similarly to the ‘get method’, but the head method response doesn't have the body, but it includes the header, like content length and type information. The HTTP head method is faster than the get method.

requests.head(url, args)

The patch method is an HTTP method used to modify an existing resource through a request. This approach helps minimize errors by enabling partial updates to the resource. The current request includes a set of guidelines outlining how to modify the existing version of the resource.

requests.head(https://panel.sendcloud.sc/api/v2/user/addresses/sender,  auth=(public_key, private_key),)

6. Patch

The patch request has a smaller bandwidth.

response = requests.patch(url)

7. options

requests.options() returns a Response object. It contains all the data and properties, such as response content, headers, encoding, cookies, etc.

Here, we have discussed important methods of the Python request library.

To read more about How to Use Python to Scrape Data From Website & Save It to Excel, refer to our blog How to Use Python to Scrape Data From Website & Save It to Excel.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message