Using The Requests Library (2024)

What is The requests Library?

Python is “batteries included”, but sometimes the included libraries available in the standard library can be hard to understand. The standard library focuses on functionality, but not necessarily ease of use.

That’s where external libraries come in. The external requests library was developed by Kenneth Reitz to make working with APIs in Python a lot easier. He calls it “HTTP, for humans.” It’s become of the (if not the most) popular Python library!

Our First Request With The requests Library

If you didn’t install the requests library in the working with libraries chapter, you’ll need to do that first before running this code. Use: python -m pip install requests

Let’s make a request to shibe.online to get ourselves some dog pictures. Create a file called shibe.py in our pyworkshop directory and copy this:

# First thing we'll do is import the requests libraryimport requests# Define a variable with the URL of the APIapi_url = "http://shibe.online/api/shibes?count=1"# Call the root of the api with GET, store the answer in a response variable# This call will return a list of URLs that represent dog picturesresponse = requests.get(api_url)# Get the status code for the response. Should be 200 OK# Which means everything worked as expectedprint(f"Response status code is: {response.status_code}")# Get the result as JSONresponse_json = response.json()# Print it. We should see a list with one image URL.print(response_json)
(env) $ python shibe.pyResponse status code is: 200['https://cdn.shibe.online/shibes/28d7c372ea7defdb315ef845285d4ac3906ccea4.jpg']

Dealing with Errors

When dealing with HTTP requests, your first indication of error is usually the HTTP status code. You saw some of the common status codes in the last chapter. The most common status codes are probably 200 - Success, and 404 - Not found. You can find the status code in the status_code property of the response object:

# Passing in a non-existant URL will result in a 404 (not found)bad_response = requests.get("http://shibe.online/api/german-shepards")print(f"Bad Response Status Code is: {bad_response.status_code}") # Status code is 404, meaning that resource doesn’t exist.

Passing in Parameters

# We'll store our base URL here and pass in the count parameter laterapi_url = "http://shibe.online/api/shibes"params = { "count": 10}# Pass those params in with the request.api_response = requests.get(api_url, params=params)print(f"Shibe API Response Status Code is: {api_response.status_code}") # should be 200 OKjson_data = api_response.json()print("Here is a list of URLs for dog pictures:")for url in json_data: print(f"\t {url}")
$ shibe.pyShibe API Response Status Code is: 200Here is a list of URLs for dog pictures: https://cdn.shibe.online/shibes/dfb2af0b2ac1f057750da32f0ea0e154afc160cf.jpg https://cdn.shibe.online/shibes/4989daad2c805ec62b0fb09a80280ba2262f1b08.jpg https://cdn.shibe.online/shibes/a9360b8262c586af2cf53a2d68bb6ec34b87fe25.jpg https://cdn.shibe.online/shibes/a168cc7f2524c73b433afd7c02f698884738daff.jpg https://cdn.shibe.online/shibes/3fbe49908948718c521b756f31dc155ed22941f6.jpg https://cdn.shibe.online/shibes/846bb52389cf9af8a54eb12f48e0e7d0883b17da.jpg https://cdn.shibe.online/shibes/d11ed7f57c5a882f047b921a73f0b95714626bb3.jpg https://cdn.shibe.online/shibes/0fd1dcc9f5866cefaa3040de1be0f8971b0530cd.jpg https://cdn.shibe.online/shibes/cd668ca05d0ec78863f3c30b08b9cd4ff7f5669c.jpg https://cdn.shibe.online/shibes/32bf0797e5a4c5bfb6fc06edc57ddfbf4e08f98f.jpg

More about requests

To learn more about the requests library after class, look at the quick start.

Using The Requests Library (2024)
Top Articles
16.6. Managing the Certificate Database Red Hat Certificate System 9 | Red Hat Customer Portal
Ethereum Price Prediction: 2024, 2025, 2030
The Tribes and Castes of the Central Provinces of India, Volume 3
Drury Inn & Suites Bowling Green
Angela Babicz Leak
Mate Me If You May Sapir Englard Pdf
Craigslist Free Stuff Appleton Wisconsin
Lowes 385
Wmlink/Sspr
Cape Cod | P Town beach
Slmd Skincare Appointment
Best Food Near Detroit Airport
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Hood County Buy Sell And Trade
House Party 2023 Showtimes Near Marcus North Shore Cinema
A rough Sunday for some of the NFL's best teams in 2023 led to the three biggest upsets: Analysis - NFL
Kvta Ventura News
Navy Female Prt Standards 30 34
Pizza Hut In Dinuba
Adam4Adam Discount Codes
Red Devil 9664D Snowblower Manual
Earl David Worden Military Service
Concordia Apartment 34 Tarkov
Teacup Yorkie For Sale Up To $400 In South Carolina
Transactions (zipForm Edition) | Lone Wolf | Real Estate Forms Software
Riherds Ky Scoreboard
Isaidup
Optum Urgent Care - Nutley Photos
Terry Bradshaw | Biography, Stats, & Facts
پنل کاربری سایت همسریابی هلو
Regina Perrow
Mikayla Campinos: Unveiling The Truth Behind The Leaked Content
John Deere 44 Snowblower Parts Manual
Motor Mounts
James Ingram | Biography, Songs, Hits, & Cause of Death
Aladtec Login Denver Health
Beth Moore 2023
#scandalous stars | astrognossienne
Truckers Report Forums
Barrage Enhancement Lost Ark
Bimmerpost version for Porsche forum?
3302577704
When His Eyes Opened Chapter 2048
Google Flights Orlando
Final Fantasy 7 Remake Nexus
Nail Salon Open On Monday Near Me
Alpha Labs Male Enhancement – Complete Reviews And Guide
Makes A Successful Catch Maybe Crossword Clue
Oakley Rae (Social Media Star) – Bio, Net Worth, Career, Age, Height, And More
Union Supply Direct Wisconsin
Cool Math Games Bucketball
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 6317

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.