Python · API · Intermediate

User Profile

5 tasks

Read and update a user profile with GET, PATCH, and PUT requests.

GET, PATCH, and PUT — when to use each

#
The profile endpoint exposes the authenticated user's own account data. It supports three HTTP methods — each with a different update semantics: - GET — read the current profile - PATCH — partial update: send only the fields you want to change - PUT — full replacement: send all fields, even the ones you are not changing The difference between PATCH and PUT matters: PATCH is surgical. If you only want to change the first name, send just first_name. The server keeps everything else as-is: ```python requests.patch(url, json={'first_name': 'Alice'}, headers=headers) # email, last_name, etc. are unchanged ``` PUT is a full replacement. The server treats the body as the complete new state of the resource. Any field you omit may be reset to a default or cause a validation error: ```python requests.put(url, json={ 'first_name': 'Alice', 'last_name': 'Smith', 'email': '[email protected]', }, headers=headers) # ALL fields replaced ``` In practice, PATCH is almost always what you want for profile updates — it is safer because you cannot accidentally erase data you forgot to include. The profile object includes these fields: id, username, email, first_name, last_name, phone_number, and image. The phone_number and image fields are null by default and can be updated via PATCH or PUT.

Reading and updating the profile

#
Read the current profile: ```python import requests BASE_URL = 'https://apilearn.tukas.dev' token = requests.post(f'{BASE_URL}/api/auth/token/', json={ 'username': 'alice42', 'password': 'securepass123', }).json()['token'] headers = {'Authorization': f'Token {token}'} profile = requests.get(f'{BASE_URL}/api/users/profile/', headers=headers).json() print(profile) # {'id': 7, 'username': 'alice42', 'email': '', 'first_name': '', 'last_name': '', 'phone_number': None, 'image': None} ``` PATCH to change just the first name: ```python response = requests.patch( f'{BASE_URL}/api/users/profile/', json={'first_name': 'Alice'}, headers=headers, ) print(response.json()['first_name']) # 'Alice' ``` Verify the change with another GET: ```python profile = requests.get(f'{BASE_URL}/api/users/profile/', headers=headers).json() print(profile['first_name']) # 'Alice' ``` PUT to fully replace the profile (all fields required): ```python response = requests.put( f'{BASE_URL}/api/users/profile/', json={ 'first_name': 'Alice', 'last_name': 'Smith', 'email': '[email protected]', }, headers=headers, ) print(response.status_code) # 200 print(response.json()) ``` A useful pattern — write a function that wraps profile update so any call site just passes keyword args: ```python def update_profile(headers, **fields): return requests.patch( f'{BASE_URL}/api/users/profile/', json=fields, headers=headers, ).json() # Update only email update_profile(headers, email='[email protected]') # Update name and email together update_profile(headers, first_name='Alice', last_name='Smith', email='[email protected]') ```

Profile endpoint reference

#
Profile endpoint (requires auth): ``` GET /api/users/profile/ Read profile PATCH /api/users/profile/ Partial update (only send changed fields) PUT /api/users/profile/ Full replacement (send all fields) ``` Profile fields: ``` id int Read-only username string Read-only after registration email string Updateable first_name string Updateable last_name string Updateable phone_number string Updateable (null by default) image string Updateable (null by default) ``` PATCH vs PUT: ``` PATCH Send only what changes Other fields preserved PUT Send everything Omitted fields may reset ``` Example: ```python # PATCH — only email changes requests.patch(url, json={'email': '[email protected]'}, headers=headers) # PUT — must send all updatable fields requests.put(url, json={ 'first_name': 'Alice', 'last_name': 'Smith', 'email': '[email protected]', }, headers=headers) ```
01

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}')
02

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'])
03

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())
04

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']))
05

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)