The requests module allows you to handle HTTP requests in Python. Before using it, you will need to ensure it is installed in your environment. You can do this by running `pip install requests` in your command shell.
Here’s how you can use it for various purposes:
1. Import it: You will need to import the module before you can start using it.
\`\`\`python
import requests
\`\`\`
1. Making a GET request: This involves retrieving data from a specific resource.
\`\`\`python
response = requests.get(‘https://www.google.com’)
\`\`\`
1. Viewing the response text
\`\`\`python
print(response.text)
\`\`\`
1. Making a POST request: This involves sending data to a specific resource.
\`\`\`python
response = requests.post(‘https://httpbin.org/post’, data = {‘key’:‘value’})
\`\`\`
1. Sending query parameters to a URL
\`\`\`python
payload = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
response = requests.get(‘https://httpbin.org/get’, params=payload)
\`\`\`
1. Viewing the full URL that was used:
\`\`\`python
print(response.url)
\`\`\`
1. Sending and handling JSON data
\`\`\`python
response = requests.post(‘https://httpbin.org/post’, json = {‘key’:‘value’})
json\_response = response.json()
print(json\_response[‘data’])
print(json\_response\[‘headers’]\[‘Content-Type’])
\`\`\`
Remember to check the status of your response to ensure your request was successful.
```
print(response.status_code)
```
A status code of 200 signifies success.
Note: While the requests library is incredibly useful and easy to use, please ensure that you are respectful and ethical in your requests to avoid overloading servers or breaking any terms of service.