Skip to main content

Python - Working with REST API Requests

Sham Sui Po, Hong Kong

Github Repository

Connecting to a REST API (e.g. The Cocktail DB) using Python:

curl 'https://www.thecocktaildb.com/api/json/v1/1/search.php?s=Gin%20Tonic'
{"drinks":[{"idDrink":"178365","strDrink":"Gin Tonic","strDrinkAlternate":null,"strTags":null,"strVideo":null,"strCategory":"Cocktail","strIBA":null,"strAlcoholic":"Alcoholic","strGlass":"Highball glass","strInstructions":"Fill a highball glass with ice, pour the gin, top with tonic water and squeeze a lemon wedge and garnish with a lemon wedge.","strInstructionsES":null,"strInstructionsDE":"F\u00fcllen Sie ein Highball-Glas mit Eis, gie\u00dfen Sie den Gin ein, gie\u00dfen Sie Tonic Water dar\u00fcber und dr\u00fccken Sie eine Zitronenscheibe aus und garnieren Sie sie mit einer Zitronenscheibe.","strInstructionsFR":null,"strInstructionsIT":"Colmate un bicchiere highball di ghiaccio, versate il gin, colmate con acqua tonica e spremete uno spicchio di limone e guarnite con una fetta di limone. ","strInstructionsZH-HANS":null,"strInstructionsZH-HANT":null,"strDrinkThumb":"https:\/\/www.thecocktaildb.com\/images\/media\/drink\/qcgz0t1643821443.jpg","strIngredient1":"Gin","strIngredient2":"Tonic Water","strIngredient3":"Lemon Peel","strIngredient4":"Ice","strIngredient5":null,"strIngredient6":null,"strIngredient7":null,"strIngredient8":null,"strIngredient9":null,"strIngredient10":null,"strIngredient11":null,"strIngredient12":null,"strIngredient13":null,"strIngredient14":null,"strIngredient15":null,"strMeasure1":"4 cl","strMeasure2":"10 cl","strMeasure3":"1 Slice","strMeasure4":"cubes","strMeasure5":"","strMeasure6":"","strMeasure7":"","strMeasure8":null,"strMeasure9":null,"strMeasure10":null,"strMeasure11":null,"strMeasure12":null,"strMeasure13":null,"strMeasure14":null,"strMeasure15":null,"strImageSource":"https:\/\/www.brunovanzan.com\/wp-content\/uploads\/2020\/01\/gin-tonic.png","strImageAttribution":null,"strCreativeCommonsConfirmed":"No","dateModified":null}]}

Request

import requests

r = requests.get('https://www.thecocktaildb.com/api/json/v1/1/search.php?s=Gin%20Tonic')
drink = r.json()['drinks'][0]

for item in drink:
print('NAME:\n', drink['strDrink'], '\nINSTRUCTIONS:\n', drink['strInstructions'], '\nIMAGE:\n', drink['strImageSource'])

# =>
# NAME:
# Gin Tonic
# INSTRUCTIONS:
# Fill a highball glass with ice, pour the gin, top with tonic water and squeeze a lemon wedge and garnish with a lemon wedge.
# IMAGE:
# https://www.brunovanzan.com/wp-content/uploads/2020/01/gin-tonic.png

With a user defined search request:

import requests
from pathlib import Path

# user defined search request

def get_drink(drink):
url = f'https://www.thecocktaildb.com/api/json/v1/1/search.php?s={drink}'
r = requests.get(url)
drink = r.json()['drinks'][0]
with open(results_path, 'a') as file:
file.write(f"{drink['strDrink']}, {drink['strInstructions']}, {drink['strImageSource']}"+'\n')
return


# prepare CSV file to store results

results_path = Path('files/drinks.csv')

content = "NAME, INSTRUCTIONS, IMAGE"

if not results_path.exists():
with open(results_path, 'w') as file:
file.write("NAME, INSTRUCTIONS, IMAGE\n")

# run search

get_drink(drink='Daiquiri')
NAME, INSTRUCTIONS, IMAGE
Daiquiri, Pour all ingredients into shaker with ice cubes. Shake well. Strain in chilled cocktail glass., https://commons.wikimedia.org/wiki/File:Classic_Daiquiri_in_Cocktail_Glass.jpg