JavaScript · API · Средний
Профиль пользователя
Читайте и обновляйте профиль с помощью GET, PATCH и PUT-запросов.
Краткое введение в тему и пояснения перед упражнениями (упражнения ниже):
Чтение и обновление профиля
#Справочник эндпоинта профиля
#Упражнения:
Прочитать профиль
#Авторизуйтесь и отправьте GET `/api/users/profile/`. Выведите `username` и `email`.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// GET /api/users/profile/ и вывести username и email
}
main();
Решение
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const profile = await fetch(`${BASE_URL}/api/users/profile/`, {
headers: { 'Authorization': `Token ${token}` },
}).then(r => r.json());
console.log(profile.username);
console.log(profile.email);
}
main();
Обновить имя через PATCH
#Прочитайте текущий профиль, затем обновите `first_name` через PATCH. Выведите новое `first_name` и подтвердите, что `email` не изменился.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// 1. авторизоваться
// 2. прочитать профиль
// 3. PATCH first_name
// 4. вывести новое first_name и подтвердить что email не изменился
}
main();
Решение
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const PROFILE_URL = `${BASE_URL}/api/users/profile/`;
const before = await fetch(PROFILE_URL, { headers }).then(r => r.json());
console.log('Before:', before.first_name);
const after = await fetch(PROFILE_URL, {
method: 'PATCH', headers,
body: JSON.stringify({ first_name: 'Updated' }),
}).then(r => r.json());
console.log('After:', after.first_name);
console.log('Email unchanged:', after.email === before.email);
}
main();
Обновить несколько полей сразу
#За один PATCH-запрос обновите `first_name`, `last_name` и `phone_number`. Выведите все три обновлённых значения.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// PATCH 3 поля сразу
}
main();
Решение
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const updated = await fetch(`${BASE_URL}/api/users/profile/`, {
method: 'PATCH', headers,
body: JSON.stringify({
first_name: 'Test',
last_name: 'User',
phone_number: '+1112223333',
}),
}).then(r => r.json());
console.log(updated.first_name);
console.log(updated.last_name);
console.log(updated.phone_number);
}
main();
Полная замена через PUT
#Прочитайте текущий профиль, затем отправьте PUT со всеми полями, но измените `email`. Выведите обновлённый email для подтверждения.
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
// PUT требует все поля — сначала прочитайте!
}
main();
Решение
const BASE_URL = 'https://apilearn.tukas.dev';
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const PROFILE_URL = `${BASE_URL}/api/users/profile/`;
const current = await fetch(PROFILE_URL, { headers }).then(r => r.json());
const updated = await fetch(PROFILE_URL, {
method: 'PUT', headers,
body: JSON.stringify({
username: current.username,
email: '[email protected]',
first_name: current.first_name || '',
last_name: current.last_name || '',
phone_number: current.phone_number || '',
}),
}).then(r => r.json());
console.log(updated.email);
}
main();
Функция updateProfile()
#Напишите async-функцию `updateProfile(headers, fields)`, которая PATCHит профиль и возвращает результат. Вызовите её дважды с разными полями.
const BASE_URL = 'https://apilearn.tukas.dev';
async function updateProfile(headers, fields) {
// PATCH /api/users/profile/ с fields, вернуть результат
}
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
// вызвать updateProfile дважды с разными полями
}
main();
Решение
const BASE_URL = 'https://apilearn.tukas.dev';
async function updateProfile(headers, fields) {
return fetch(`${BASE_URL}/api/users/profile/`, {
method: 'PATCH',
headers,
body: JSON.stringify(fields),
}).then(r => r.json());
}
async function main() {
const { token } = await fetch(`${BASE_URL}/api/auth/token/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD' }),
}).then(r => r.json());
const headers = {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json',
};
const r1 = await updateProfile(headers, { first_name: 'Test' });
console.log(r1.first_name);
const r2 = await updateProfile(headers, { phone_number: '+9998887777' });
console.log(r2.phone_number);
}
main();