You can download this code by clicking the button below.
This code is now available for download.
This function uses the urllib3 library to fetch information about a random user from the https://randomuser.me/ API. It specifies the user by the user_id parameter.
Technology Stack : urllib3, json
Code Type : Function
Code Difficulty : Intermediate
def fetch_random_user(user_id):
import urllib3
import json
from urllib3.exceptions import HTTPError
http = urllib3.PoolManager()
url = f"https://randomuser.me/api/?id={user_id}"
try:
response = http.request('GET', url)
data = json.loads(response.data.decode('utf-8'))
return data
except HTTPError as e:
print(f"An error occurred: {e}")
return None
# JSON explanation