Categories:
#web
#database
Tags:
#python
#requests
#sqlite
Over the years I’ve had to get overly familiar with API requests, and have reimplemented the same design pattern across multiple projects involving the retrieval, processing and display of API data.
This article is a collection of these common workflows.
In Python
🐍
1import requests
2import pandas as pd
3import json
Links:
https://requests.readthedocs.io/en/latest/api/
https://requests.readthedocs.io/en/latest/api/
Interactive Notebooks:
../../notebooks/
1endpoint = "michaelmuratov.com"
2response = requests.get(endpoint)
3data = response.json()
The endpoint variable holds the URL of the API I want to query. In this case, “http://www.datasource.com”.
I use requests.get(endpoint) to send an HTTP GET request to that URL.
The server responds, and the response is stored in the response variable. Since most APIs today communicate using JSON, I make the automatic conversion of the text data into a parsable format.
1from requests.auth import HTTPBasicAuth
2import base64
3
4#with library
5response = requests.get(endpoint, auth=HTTPBasicAuth('user', 'pass'))
6
7#manually
8auth_string = "userid:password"
9b64Val = base64.b64encode(auth_string)
1
2proxies = {
3 "http" : http_proxy,
4 "https" : https_proxy,
5}
6
7r = requests.get(endpoint, proxies=proxies)
1
2#no SSL verification
3requests.get(endpoint, verify=False)
4
5#Use default SSL verification
6requests.get(endpoint, verify=True)
7
8#Use custom SSL verification
9requests.get(endpoint, verify=True, cert="cert.pem")
Launching my AI Web Security Lab — Here’s Why I’m Building It
And you don’t need to memorize how binary search works