Guides › Your First GET Request: Browser, curl and Python

Lesson 5 of 8 2 min read All lessons

Your First GET Request: Browser, curl and Python

A GET request asks a server for data without changing anything. Here is the same request three ways, using the free practice API.

1. In your browser

Paste this address into a new tab: /v1/get/name. You will see a small piece of JSON.

2. With curl

curl https://api.computewithpour.com/v1/get/name

3. With Python

import requests

r = requests.get("https://api.computewithpour.com/v1/get/name")
print(r.status_code)   # 200
print(r.json())        # for example {'id': 4, 'name': 'Guido'} (the name is random, yours will differ)

What to notice

The status code tells you whether it worked. The body is JSON, which Python reads as a dictionary. Next, add a query parameter: /v1/names?limit=3.

Challenge

Write a Python script that fetches every name using limit=5 and prints how many requests it took.

Show the answer

Loop with offset until it reaches total. There are 12 names, so it takes 3 requests (pages of 5, 5 and 2).

import requests

base = "https://api.computewithpour.com"
names, offset, made = [], 0, 0
while True:
    r = requests.get(f"{base}/v1/names", params={"limit": 5, "offset": offset})
    made += 1
    data = r.json()
    names += [n["name"] for n in data["results"]]
    offset += 5
    if offset >= data["total"]:
        break
print(made, names)   # 3 requests, 12 names