Building an HTTP client with Request and RESTful API in python

Building an HTTP client with Request and RESTful API in python

The requests module in Python is a popular library for making HTTP requests, and it is commonly used for working with RESTful APIs. RESTful APIs (Representational State Transfer APIs) are web services that adhere to the principles of REST and allow clients to interact with server resources over HTTP.Overview of using the requests module to work with RESTful APIs in Python:

Installing the requests module To use the requests module, you’ll need to install it using pip:

Bash
pip install requests

Making a GET request To make a GET request to an API endpoint, you can use the requests.get() method. For example, to get a list of users from a JSON API, you would do the following:

Python
import requests

response = requests.get('https://jsonplaceholder.typicode.com/users')

The response object contains the HTTP response from the API. You can access the status code of the response using the status_code property:

Python
status_code = response.status_code
print(status_code)

If the request was successful (status code 200), you can access the JSON data using the json() method:

Python
data = response.json()
print(data)

Making a POST request To make a POST request to an API endpoint, you can use the requests.post() method. For example, to create a new user in a JSON API, you would do the following:

Python
import requests

data = {
    "name": "John Doe",
    "email": "johndoe@example.com"
}

response = requests.post('https://jsonplaceholder.typicode.com/users', json=data)

The json=data parameter tells the requests module to encode the data dictionary as JSON and send it as the request body. You can access the status code of the response using the status_code property:

Python
status_code = response.status_code
print(status_code)

If the request was successful (status code 201), you can access the JSON data using the json() method:

Python
data = response.json()
print(data)

Making a PUT request To make a PUT request to an API endpoint, you can use the requests.put() method. For example, to update an existing user in a JSON API, you would do the following:

Python
import requests

data = {
    "id": 1,
    "name": "Jane Doe",
    "email": "janedoe@example.com"
}

response = requests.put('https://jsonplaceholder.typicode.com/users/1', json=data)

The json=data parameter tells the requests module to encode the data dictionary as JSON and send it as the request body. You can access the status code of the response using the status_code property:

Python
status_code = response.status_code
print(status_code)

If the request was successful (status code 200), you can access the JSON data using the json() method:

Python
data = response.json()
print(data)

Making a DELETE request To make a DELETE request to an API endpoint, you can use the requests.delete() method. For example, to delete an existing user in a JSON API, you would do the following:

Python
import requests

response = requests.delete('https://jsonplaceholder.typicode.com/users/1')

You can access the status code of the response using the status_code property:

Python
status_code = response.status_code
print(status_code)

If the request was successful (status code 204), there will be no JSON data in the response.

The Requests module provides a wide range of other features for interacting with RESTful APIs, such as:

For more information on using the Requests module, please refer to the official documentation: https://requests.readthedocs.io/en/latest/

The requests module to send a GET request to a specified URL (hostname) and then prints information about the response, including the JSON content, status code, response headers, and request headers.

Import Statements

Bash
import requests
import json

User Input

Bash
domain = input("Enter the hostname http://")

Sending a GET Request

Bash
response = requests.get("http://" + domain)

Printing JSON Content

Bash
print(response.json)

Printing Status Code

Bash
print("Status code: " + str(response.status_code))

Printing Response Headers

Bash
print("Headers response: ")
for header, value in response.headers.items():
    print(header, '-->', value)

Printing Request Headers

Bash
print("Headers request : ")
for header, value in response.request.headers.items():
    print(header, '-->', value)
Bash
#!/usr/bin/env python3

import requests, json


domain = input("Enter the hostname http://")

response = requests.get("http://"+domain)

print(response.json)

print("Status code: "+str(response.status_code))

print("Headers response: ")
for header, value in response.headers.items():
  print(header, '-->', value)
  
print("Headers request : ")
for header, value in response.request.headers.items():
  print(header, '-->', value)

Obtaining only keys from header

Bash
import requests

if __name__ == "__main__":
    domain = input("Enter the hostname http://")
    response = requests.get("http://"+domain)
    for header in response.headers.keys():
        print(header  + ":" + response.headers[header])

This script takes user input for a hostname, sends a GET request to the specified URL using the requests module, and prints information about the response, including the JSON content (if corrected), status code, response headers, and request headers. The code provides insights into the communication between the client and the server for the given URL.

Getting images and links from a URL with request

Extract and display images and links from the HTML content of a given URL. It uses the requests module to fetch the HTML content and regular expressions to identify and extract image sources (<img>) and links (<a>).

Import Statements

Bash
import requests
import re

User Input for URL

Bash
url = input("Enter URL > ")

Sending a GET Request and Retrieving HTML Content

Bash
var = requests.get(url).text

Extracting and Printing Images

Bash
print("Images:")
print("#########################")
for image in re.findall("<img (.*)>", var):
    for images in image.split():
        if re.findall("src=(.*)", images):
            image = images[:-1].replace("src=\"", "")
            if image.startswith("http"):
                print(image)
            else:
                print(url + image)

Extracting and Printing Links

Bash
print("#########################")
print("Links:")
print("#########################")
for link, name in re.findall("<a (.*)>(.*)</a>", var):
    for a in link.split():
        if re.findall("href=(.*)", a):
            url_image = a[0:-1].replace("href=\"", "")
            if url_image.startswith("http"):
                print(url_image)
            else:
                print(url + url_image)
Bash
#!/usr/bin/env python3
    
import requests
import re

url = input("Enter URL > ")
var = requests.get(url).text

print("Images:")
print("#########################")
for image in re.findall("<img (.*)>",var):
    for images in image.split():
        if re.findall("src=(.*)",images):
            image = images[:-1].replace("src=\"","")
            if(image.startswith("http")):
                print(image)
            else:
                print(url+image)

print("#########################")
print("Links:")
print("#########################")
for link,name in re.findall("<a (.*)>(.*)</a>",var):
    for a in link.split():
        if re.findall("href=(.*)",a):
            url_image = a[0:-1].replace("href=\"","")
            if(url_image.startswith("http")):
                print(url_image)
            else:
                print(url+url_image)
Bash
Enter URL > https://www.awjunaid.com 

The script takes user input for a URL, fetches the HTML content using the requests module, and then uses regular expressions to extract and print images and links found in the HTML. The script provides a basic way to analyze the structure of a webpage and identify images and links. Keep in mind that using regular expressions for parsing HTML can be fragile, and more robust solutions like HTML parsers are recommended for complex scenarios.

Making GET request with the REST API

The requests module to make an HTTP GET request to “http://httpbin.org/get.” It then prints information about the response, including the HTTP status code, response headers, and specific details obtained from the response content (in JSON format).

Import Statements

Python
import requests
import json

Making an HTTP GET Request

Python
response = requests.get("http://httpbin.org/get", timeout=5)

Printing HTTP Status Code

Python
print("HTTP Status Code: " + str(response.status_code))

Printing Response Headers

Python
print(response.headers)

Checking Status Code and Processing JSON Content

Python
if response.status_code == 200:
    results = response.json()
    for result in results.items():
        print(result)

Printing Specific Response Headers

Python
print("Headers response: ")
for header, value in response.headers.items():
    print(header, '-->', value)

Printing Request Headers

Python
print("Headers request : ")
for header, value in response.request.headers.items():
    print(header, '-->', value)

Printing Server Information

Python
print("Server:" + response.headers['server'])

Handling Error Responses

Python
else:
    print("Error code %s" % response.status_code)
Python
import requests, json

response = requests.get("http://httpbin.org/get",timeout=5)

print("HTTP Status Code: " + str(response.status_code))
print(response.headers)

if response.status_code == 200:

	results = response.json()
	for result in results.items():
		print(result)

	print("Headers response: ")
	for header, value in response.headers.items():
		print(header, '-->', value)

	print("Headers request : ")
	for header, value in response.request.headers.items():
		print(header, '-->', value)

	print("Server:" + response.headers['server'])
else:
	print("Error code %s" % response.status_code)

This script sends an HTTP GET request to “http://httpbin.org/get,” retrieves and processes the response. It prints information such as the HTTP status code, response headers, specific details from the JSON content, and server information if the response is successful (status code 200). If there is an error, it prints an error message with the status code.

Making POST request with the REST API

Sending an HTTP POST request to “http://httpbin.org/post” with JSON data. It utilizes the requests module to make the request and includes headers for specifying the content type and expected response type.

Import Statements

Python
import requests
import json

POST Request with JSON Data

Python
data_dictionary = {"id": "0123456789"}
headers = {"Content-Type": "application/json", "Accept": "application/json"}
response = requests.post("http://httpbin.org/post", data=data_dictionary, headers=headers, json=data_dictionary)

Printing HTTP Status Code

Python
print("HTTP Status Code: " + str(response.status_code))

Printing Response Headers

Python
print(response.headers)

Checking Status Code and Processing JSON Content

Python
if response.status_code == 200:
    results = response.json()
    for result in results.items():
        print(result)

Printing Specific Response Headers

Python
print("Headers response: ")
for header, value in response.headers.items():
    print(header, '-->', value)

Printing Request Headers

Python
print("Headers request : ")
for header, value in response.request.headers.items():
    print(header, '-->', value)

Printing Server Information

Python
print("Server:" + response.headers['server'])

Handling Error Responses

Python
else:
    print("Error code %s" % response.status_code)
Python
#!/usr/bin/env python3

import requests,json
data_dictionary = {"id": "0123456789"}
headers = {"Content-Type" : "application/json","Accept":"application/json"}
response = requests.post("http://httpbin.org/post",data=data_dictionary,headers=headers,json=data_dictionary)
print("HTTP Status Code: " + str(response.status_code))

print(response.headers)

if response.status_code == 200:

	results = response.json()
	for result in results.items():
		print(result)

	print("Headers response: ")
	for header, value in response.headers.items():
		print(header, '-->', value)

	print("Headers request : ")
	for header, value in response.request.headers.items():
		print(header, '-->', value)

	print("Server:" + response.headers['server'])
else:
	print("Error code %s" % response.status_code)

This script sends an HTTP POST request to “http://httpbin.org/post” with JSON data, retrieves and processes the response. It prints information such as the HTTP status code, response headers, specific details from the JSON content, and server information if the response is successful (status code 200). If there is an error, it prints an error message with the status code.

Managing a proxy with requests

An intresitng feature offered by the requsest module is the option to make the request through a proxy or intermediate machine between our internal network and the external network.

Python
import requests

proxy_url = "http://proxy.example.com:8080"
auth = ("username", "password")

response = requests.get('https://api.example.com/data', proxies={"http": proxy_url, "https": proxy_url}, auth=auth)

The proxies dictionary defines the proxy URLs for both HTTP and HTTPS requests. The auth parameter provides basic authentication credentials for the proxy server.

When working with the requests library in Python, you may need to use a proxy for making HTTP requests. A proxy acts as an intermediary between your client and the destination server. Here’s how you can manage a proxy with requests:

Using a Proxy

To use a proxy with requests, you can pass the proxies parameter to the request methods. The proxies parameter should be a dictionary with the protocol (http, https, etc.) as the key and the proxy URL as the value.

Python
import requests

# Define the proxy URL
proxy_url = "http://your_proxy_url"

# Specify the proxy in the request
proxies = {
    "http": proxy_url,
    "https": proxy_url,
}

# Make a request using the proxy
response = requests.get("http://example.com", proxies=proxies)

# Print the response
print(response.text)

Proxy with Authentication

If your proxy requires authentication, you can include the username and password in the proxy URL. The format is http://username:password@proxy_url.

Python
import requests

# Define the proxy URL with authentication
proxy_url = "http://username:password@your_proxy_url"

# Specify the proxy in the request
proxies = {
    "http": proxy_url,
    "https": proxy_url,
}

# Make a request using the proxy
response = requests.get("http://example.com", proxies=proxies)

# Print the response
print(response.text)

Exceptions and Timeouts with Proxies

When using proxies, you may encounter exceptions or longer response times. It’s a good practice to handle these situations gracefully.

Python
import requests

# Define the proxy URL
proxy_url = "http://your_proxy_url"

# Specify the proxy in the request
proxies = {
    "http": proxy_url,
    "https": proxy_url,
}

try:
    # Make a request using the proxy with a timeout
    response = requests.get("http://example.com", proxies=proxies, timeout=10)

    # Raise an exception for bad status codes
    response.raise_for_status()

    # Print the response
    print(response.text)

except requests.exceptions.RequestException as e:
    print(f"Error: {e}")

In this example:

Remember to replace "http://your_proxy_url" with the actual URL of your proxy. If the proxy requires authentication, include the username and password in the URL as demonstrated.

Using proxies can be helpful for various purposes, such as:

However, it’s important to note that using proxies can also slow down your requests and introduce potential security risks. It’s essential to choose a reputable proxy provider and ensure that your proxy settings are secure.

Exit mobile version