Python · API · Intermediate
User Profile
Read and update a user profile with GET, PATCH, and PUT requests.
Quick topic start and explanations before exercises (exercises below):
Reading and updating the profile
#Profile endpoint reference
#Exercises:
Read your profile
#Authenticate and GET /api/users/profile/. Print all fields in the response.
import requests BASE_URL = 'https://apilearn.tukas.dev' # Authenticate and print all profile fields
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
profile = requests.get(f'{BASE_URL}/api/users/profile/', headers=headers).json()
for key, value in profile.items():
print(f'{key}: {value}')
Update with PATCH
#PATCH /api/users/profile/ to update just the first_name field. Then verify the change with a GET and print the updated first_name.
import requests BASE_URL = 'https://apilearn.tukas.dev' # PATCH first_name, then verify with GET
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
url = f'{BASE_URL}/api/users/profile/'
requests.patch(url, json={'first_name': 'Alex'}, headers=headers)
profile = requests.get(url, headers=headers).json()
print(profile['first_name'])
Full update with PUT
#Use PUT to set first_name, last_name, and email on your profile. Print the full response to confirm all three were saved.
import requests BASE_URL = 'https://apilearn.tukas.dev' # PUT all three fields and print the response
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
response = requests.put(
f'{BASE_URL}/api/users/profile/',
json={
'first_name': 'Jordan',
'last_name': 'Lee',
'email': '[email protected]',
},
headers=headers,
)
print(response.json())
Revert a field
#Set first_name to something with PATCH, then revert it to an empty string with another PATCH. Verify it is empty with a final GET.
import requests BASE_URL = 'https://apilearn.tukas.dev' # Set first_name, then revert it to empty string, then verify
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
url = f'{BASE_URL}/api/users/profile/'
requests.patch(url, json={'first_name': 'Temp'}, headers=headers)
requests.patch(url, json={'first_name': ''}, headers=headers)
profile = requests.get(url, headers=headers).json()
print(repr(profile['first_name']))
Write update_profile()
#Write a function update_profile(**fields) that accepts any profile fields as keyword arguments and PATCHes only those fields. Call it twice: once to set first_name, once to set email. Print the profile after both calls.
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
def update_profile(**fields):
# PATCH only the given fields
pass
update_profile(first_name='Morgan')
update_profile(email='[email protected]')
# Print final profile
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
}).json()['token']
headers = {'Authorization': f'Token {token}'}
def update_profile(**fields):
return requests.patch(
f'{BASE_URL}/api/users/profile/',
json=fields,
headers=headers,
).json()
update_profile(first_name='Morgan')
update_profile(email='[email protected]')
profile = requests.get(f'{BASE_URL}/api/users/profile/', headers=headers).json()
print(profile)