You can download this code by clicking the button below.
This code is now available for download.
This function randomly selects a URL from a predefined list, sends a GET request using the requests library, and returns the response data.
Technology Stack : requests
Code Type : Function
Code Difficulty : Intermediate
import random
import requests
def fetch_random_url():
urls = ["https://jsonplaceholder.typicode.com/todos/1", "https://api.github.com/users", "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"]
url = random.choice(urls)
response = requests.get(url)
return response.json()