# Getting Data with requests

The requests package sends a program's request to a web API with requests.get, a User-Agent header and a timeout, and gives back a response whose status_code says how it went and whose json() turns the answer into dictionaries and lists, which for the National Weather Service takes two steps, the point's forecast address and then the forecast.

Apprentice · Python · REST API · Web Development · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Send a GET request with requests.get, a User-Agent header and a timeout, and read the response's status code and JSON
- Use one request's JSON answer to build the next request, such as the forecast address inside the Weather Service's points answer

## Key takeaways

- requests is a package you install with pip in the project's virtual environment. requests.get(url) sends a GET request and gives back a response.
- headers= sends extra lines with the request, such as the User-Agent the Weather Service asks for, and timeout= says how many seconds to wait before giving up.
- response.status_code is the status code, and response.json() turns a JSON answer into Python dictionaries and lists.
- The Weather Service's forecast takes two requests, the point first, whose answer holds the forecast's address, then the forecast itself.

Opening the forecast in a browser works once. A program that checks it for you, every time you plan a trip or every morning before a report goes out, has to send the request itself and read the answer as data. In Python, that's the job of a package called **requests**, the one most Python programmers reach for when a program talks to a web API.

The example is still the weekend at Pine Ridge Campground, whose spot on the map is latitude `44.8783`, longitude `-122.6534`.

## Install requests

requests doesn't come with Python, so it goes in the project's virtual environment, the way *Virtual Environments and pip* sets one up. With the environment on (the prompt starts with `(.venv)`):

```bash
pip install requests
```

A project pins the version it was tested with in its `requirements.txt`, as `requests==2.34.2`, so `pip install -r requirements.txt` gives every computer the same one. Python has its own tools for web requests too (`urllib.request`), but requests takes fewer lines and gives clearer errors, which is why most projects use it.

## Your first request

Here's the campground's points request from *What a Web API Is*, sent by a program:

```python
import requests

url = "https://api.weather.gov/points/44.8783,-122.6534"
headers = {"User-Agent": "PythonCourseExample/1.0 (example@example.com)"}

response = requests.get(url, headers=headers, timeout=10)
print(response.status_code)

data = response.json()
print(data["properties"]["forecast"])
print(data["properties"]["relativeLocation"]["properties"]["city"])
```

```text
200
https://api.weather.gov/gridpoints/PQR/107,74/forecast
Mehama
```

Line by line:

- **`requests.get(url, ...)`** sends a `GET` request to the address and waits for the answer. What comes back is a *response* object.
- **`headers=`** sends extra lines of information along with the request, like the return address on an envelope. Each **header** is a name and a value, so they go in a dictionary. The Weather Service asks every program to send a **`User-Agent`** header that names the app and a way to reach its author, so it can tell programs apart. Make one up for your own program; `example.com` is a domain kept for examples.
- **`timeout=10`** says to wait at most 10 seconds for the server. Without it, a program can wait forever for a server that never answers.
- **`response.status_code`** is the status code: `200`, so it worked.
- **`response.json()`** turns the JSON text into dictionaries and lists, the way `json.loads()` does, so you follow the keys with square brackets. Mehama is the town nearest the campground.

## Two steps to a forecast

The points answer doesn't hold the forecast; it holds the forecast's address, the office and map square (`PQR/107,74`) that cover the spot. The Weather Service can redraw its map squares, so a program asks for the address every time instead of typing it in. That makes two requests, the second built from the first one's answer:

```python
import requests

headers = {"User-Agent": "PythonCourseExample/1.0 (example@example.com)"}
weekend = ["Saturday", "Saturday Night", "Sunday", "Sunday Night"]


def get_json(url):
    response = requests.get(url, headers=headers, timeout=10)
    return response.json()


points = get_json("https://api.weather.gov/points/44.8783,-122.6534")
forecast = get_json(points["properties"]["forecast"])

for period in forecast["properties"]["periods"]:
    if period["name"] in weekend:
        rain = period["probabilityOfPrecipitation"]["value"]
        print(f"{period['name']}: {period['temperature']}°F, {rain}% chance of rain")
```

On the Tuesday this lesson was written, it printed:

```text
Saturday: 43°F, 74% chance of rain
Saturday Night: 38°F, 74% chance of rain
Sunday: 45°F, 50% chance of rain
Sunday Night: 38°F, 40% chance of rain
```

Yours shows the forecast for the day you run it. The forecast's `periods` start with today or tonight and run a week ahead, each a half day with a `name`, so the `if` keeps only the four weekend periods. `get_json` does the asking in one place, which the next lesson builds on.

![Two requests to the National Weather Service, one after the other: the first asks api.weather.gov for the points of 44.8783,-122.6534 and gets back an answer whose forecast address is https://api.weather.gov/gridpoints/PQR/107,74/forecast; the second asks that address and gets back the forecast's periods, Saturday 43°F with a 74% chance of rain](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/533c473acc954821ab80e6d71f6d21a2.webp)

## Options with params

A query string can be typed into the URL, or requests can build it from a dictionary with `params=`, which also gets the `?`, the `=` and the `&` right. Here it asks the forecast address the points answer gave for Celsius:

```python
import requests

headers = {"User-Agent": "PythonCourseExample/1.0 (example@example.com)"}
url = "https://api.weather.gov/gridpoints/PQR/107,74/forecast"

response = requests.get(url, params={"units": "si"}, headers=headers, timeout=10)
print(response.url)
print(response.json()["properties"]["periods"][0]["temperatureUnit"])
```

```text
https://api.weather.gov/gridpoints/PQR/107,74/forecast?units=si
C
```

`response.url` is the address requests sent, query string and all, and the temperatures came back in Celsius.

## Check yourself

2 questions to try on the lesson page.

## So what

`requests.get` with a header and a timeout, a status code to check and `.json()` to read: those few lines are how a Python program fetches data from almost any web API.

## Key terms

- [requests](https://www.ask-codey.com/glossary/requests)
- [Header](https://www.ask-codey.com/glossary/header-web-development)
- [User-Agent](https://www.ask-codey.com/glossary/user-agent)
- [Endpoint](https://www.ask-codey.com/glossary/endpoint-rest-api)
- [GET](https://www.ask-codey.com/glossary/get)

## Related

- [What a Web API Is](https://www.ask-codey.com/lessons/python-web-apis-what-is-an-api)
- [Virtual Environments and pip](https://www.ask-codey.com/lessons/python-venv-and-pip)
- [When a Request Fails](https://www.ask-codey.com/lessons/python-requests-when-it-fails)

Source: https://www.ask-codey.com/lessons/python-requests-getting-json
