Many APIs require authentication to access their endpoints. You can also customize the headers of your requests to send additional information.
Key Concepts
- Authentication: *Use the auth parameter to handle basic HTTP authentication.
- Custom Headers: Pass custom headers using the headers parameter.
Basic Authentication:
from requests.auth import HTTPBasicAuth
response = requests.get('https://api.example.com/secure', auth=HTTPBasicAuth('username', 'password'))
print(response.status_code)Custom Headers:
headers = {'User-Agent': 'MyApp/1.0'}
response = requests.get('https://api.example.com/data', headers=headers)
print(response.headers)Send a GET request to an API that requires basic authentication.
Write a script to send a GET request with custom headers and print the response.
Use an API key to authenticate and retrieve data from a public API. For example at demo.nautobot.com.