Making Web Requests in Python
Making Web Requests in Python
Fringe Benefits :
web requests in Python, we need to have the following:
Installation
Run this command in bash terminal or command prompt:
Example:
code : response = requests.get('https://jsonplaceholder.typicode.com/posts/1')
Key Points:
- requests.get(url) sends a GET request to the specified URL.
- The response object holds the server’s response to the request.
Advanced Usage
1. Adding Headers
Headers are often required for authentication or providing additional information to the server.
Code : headers = {'Authorization': 'Bearer YOUR_TOKEN'}
response = requests.get('https://api.example.com/data', headers=headers)
import requests
url = 'https://api.example.com/data'
params = {'key': 'value'}
headers = {'Authorization': 'Bearer YOUR_TOKEN'}