Hypertext Transfer Protocol (HTTP) is how we send data and files around the Web
HTTP works as a request-response protocol between a client and server.
An HTTP request consists of:
curl # Command-line tool used to send HTTP requests
-X GET # Method
-H 'Accept: application/json' # Header
# Request URI
'https://api.open-meteo.com/v1/forecast?latitude=43.07&longitude=-89.40&hourly=temperature_2m'
Common request methods
GET | Retreive data from a specified resource |
POST | Send data to a server (usually via an API) to create/overwrite a resource |
PUT | Send data to a server (usually via an API) to create/update a resource |
DELETE | Delete a specified resource |
The Requests module is a third-party library, and is the de-facto standard for sending HTTP requests and receiving responses using Python.
On your own machine, you need to install requests before you can import it into Python files
pip install requests
In Replit, install requests by clicking Tools > Packages, then searching for "requests"
requests.get() sends an HTTP request and returns an HTTP response object
import requests
response = requests.get("https://girldevelopit.com")
print(vars(response))
Lots of stuff! See full list. The values we're typically concerned about are:
.status_code | Status code of the request (200 OK, 404 Not found, etc) |
.text, .json or .raw | Body of the response (stored in different attributes, depending on the type of response |
.headers | HTTP response headers, with details about the response content and where it came from |
Let's look at some response values
import requests
response = requests.get("https://girldevelopit.com")
print(response.status_code) # HTTP status code (200)
print(response.headers) # Content-type tells us what kind of response this is (text/html)
print(response.text) # The full body of the response (HTML, in this case)
An Application Programming Interface (API) is a way for 2 separate computer systems to communicate. Essentially, it's a set of rules for retrieving, creating, updating and/or deleting data in another system.
There are many different types of APIs, but on the Web we're typically talking about RESTful APIs, which allow 2 applications to exchange data securely over the Internet. RESTful APIs use URIs to identify resources.
NASA posts an Astronomy Picture of the Day each day, which is also available via an API. To use this API, you first need to register for a key at https://api.nasa.gov
After registering, you should recieve a key, along with a sample request.
In the NASA API, your key is added as a query paramter
?api_key=eIkpQYXKa6mq1J75qPMYc0Rvit3K8wNaytEJCnTn&start_date=2023-01-01
Query parameters ("params") are key/value pairs that allow adding extra context to a URL.
?
&
https://api.nasa.gov/planetary/apod?api_key=eIkpQYXKa6mq1J75qPMYc0Rvit3K8wNaytEJCnTn&start_date=2023-01-01
Params are specific to seach site/app/API - there's no universal set of available params
Let's run this in Python
import requests
# per NASA API docs, we need to pass the key as a query parameter
params = {'api_key': 'eIkpQYXKa6mq1J75qPMYc0Rvit3K8wNaytEJCnTn'}
response = requests.get("https://api.nasa.gov/planetary/apod", params=params)
print(response.status_code)
print(response.headers)
Javascript Object Notation (JSON) is an open standard file format that stores data in key/value pairs. It looks a lot like a Python dictionary!
Requests has a built-in .json()
method for parsing JSON responses, and Python has a built-in JSON library, too.
import requests
params = {'api_key': 'eIkpQYXKa6mq1J75qPMYc0Rvit3K8wNaytEJCnTn'}
response = requests.get("https://api.nasa.gov/planetary/apod", params=params)
print(response.json())
{
'copyright': 'Mike Selby',
'date': '2023-02-04',
'explanation': 'Centered in this colorful cosmic canvas, NGC 2626 is a beautiful, bright, blue reflection nebula in the southern Milky Way. Next to an obscuring dust cloud and surrounded by reddish hydrogen emission from large H II region RCW 27 it lies within a complex of dusty molecular clouds known as the Vela Molecular Ridge. NGC 2626 is itself a cloud of interstellar dust reflecting blue light from the young hot embedded star visible within the nebula. But astronomical explorations reveal many other young stars and associated nebulae in the star-forming region. NGC 2626 is about 3,200 light-years away. At that distance this telescopic field of view would span about 30 light-years along the Vela Molecular Ridge.',
'hdurl': 'https://apod.nasa.gov/apod/image/2302/NGC_2626_CDK_700_II_20_Jan_2023.jpg',
'media_type': 'image',
'service_version': 'v1',
'title': 'NGC 2626 along the Vela Molecular Ridge',
'url': 'https://apod.nasa.gov/apod/image/2302/NGC_2626_CDK_700_II_20_Jan_2023_1024.jpg'
}
Once the response values are parsed as JSON, we can treat it like a dictionary and access individual fields with bracket notation.
import requests
params = {'api_key': 'eIkpQYXKa6mq1J75qPMYc0Rvit3K8wNaytEJCnTn'}
response = requests.get("https://api.nasa.gov/planetary/apod", params=params)
json = response.json()
url = json['url']
print(url)
Requests includes custom exception types, such as HTTP error, which covers issues like 404 not found and 403 access denied (bad credentials).
Requests does not automatically raise HTTP exceptions; we need to do that explicitly with the .raise_for_status() method and handle the exception.
import requests
params = {'api_key': 'eIkpQYXKa6mq1J75qPMYc0Rvit3K8wNaytEJCnTn'}
try:
response = requests.get("https://api.nasa.gov/planetary/apo", params=params)
response.raise_for_status()
json = response.json()
url = json['url']
print(url)
except requests.exceptions.HTTPError as e:
print("Oh no, an error happened: ", e)
Software frameworks are packages of starter code that you can build on. They're often used to develop web applications quickly. Frameworks do things like:
There are many Python frameworks out there! The 2 most popular general-purpose frameworks for web applications are:
Both allow you to build web applications and/or APIs, and both have a robust extension ecosystem
In this class, we'll use Django. Its features include:
In this class, we'll use the MVT approach
pip install
, like any other package.For our project, we'll run Django in Replit. From here on out, we'll follow this tutorial:
The tutorial will guide us through building a simple Django web app the displays the current weather based on the user's location.
For next time, please make sure you've completed tutorial steps 1-3!
Also, please register for an OpenWeather API key! (step 1 in Configure OpenWeather API key)