Skip to content

Instantly share code, notes, and snippets.

@nmathira
Created June 9, 2021 22:21
Show Gist options
  • Save nmathira/ee360a78416378e2f263099bfea7e5a4 to your computer and use it in GitHub Desktop.
Save nmathira/ee360a78416378e2f263099bfea7e5a4 to your computer and use it in GitHub Desktop.
Receiving data from an API, with and without verification
import requests
def get_dog_fact():
data = requests.get("https://some-random-api.ml/facts/dog")
if data.status_code == 200:
data = data.json()
return data["fact"]
else:
return f"Request Failed with status code: {data.status_code}"
def get_dog_img():
headers = {'x-api-key': '203d078f-14a9-4835-8c17-c386b60cfdf0'}
data = requests.get("https://api.thedogapi.com/v1/images/search", headers=headers)
data = data.json()
return data[0]["url"]
if __name__ == '__main__':
# print(get_dog_fact())
print(get_dog_img())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment