Overview
The profile management endpoints allow users to update their name information and retrieve user details by email.
Update Profile
Updates a user’s nombre (first name) and apellidos (last name) fields.
Request Body
Uses the UsuarioDatos Pydantic model:
User’s email address (identifies which user to update)
Response
Success message: "Datos guardados"
Always true on successful update
{
"mensaje": "Datos guardados",
"exito": true
}
Examples
cURL
curl -X POST http://localhost:8000/actualizar_perfil \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"nombre": "María",
"apellidos": "Rodríguez"
}'
Python
import requests
url = "http://localhost:8000/actualizar_perfil"
data = {
"email": "[email protected]",
"nombre": "María",
"apellidos": "Rodríguez"
}
response = requests.post(url, json=data)
result = response.json()
if result["exito"]:
print("Profile updated successfully!")
JavaScript
const updateProfile = async (email, nombre, apellidos) => {
const response = await axios.post('http://localhost:8000/actualizar_perfil', {
email: email,
nombre: nombre,
apellidos: apellidos
});
if (response.data.exito) {
console.log('Profile updated!');
}
};
updateProfile('[email protected]', 'María', 'Rodríguez');
Get User Details
Retrieves user profile information by email address.
Path Parameters
Response
Success Response
Error Response
If the user is not found:
{
"detail": "Usuario no encontrado"
}
HTTP Status: 404 Not Found
Examples
cURL
Python
import requests
email = "[email protected]"
url = f"http://localhost:8000/conseguir_usuario/{email}"
response = requests.get(url)
if response.status_code == 200:
user = response.json()
print(f"Name: {user['nombre']} {user['apellidos']}")
print(f"Email: {user['email']}")
elif response.status_code == 404:
print("User not found")
else:
print(f"Error: {response.status_code}")
JavaScript
const getUserDetails = async (email) => {
try {
const response = await axios.get(
`http://localhost:8000/conseguir_usuario/${email}`
);
const user = response.data;
console.log(`Name: ${user.nombre} ${user.apellidos}`);
console.log(`Email: ${user.email}`);
return user;
} catch (error) {
if (error.response?.status === 404) {
console.error('User not found');
} else {
console.error('Request failed:', error.message);
}
return null;
}
};
getUserDetails('[email protected]');
React Hook Example
import { useState, useEffect } from 'react';
import axios from 'axios';
function useUserProfile(email) {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchUser = async () => {
try {
const response = await axios.get(
`http://localhost:8000/conseguir_usuario/${email}`
);
setUser(response.data);
} catch (err) {
setError(err.response?.status === 404
? 'User not found'
: 'Failed to load user'
);
} finally {
setLoading(false);
}
};
if (email) {
fetchUser();
}
}, [email]);
return { user, loading, error };
}
// Usage in component
function UserProfile({ email }) {
const { user, loading, error } = useUserProfile(email);
if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
return (
<div>
<h2>{user.nombre} {user.apellidos}</h2>
<p>Email: {user.email}</p>
</div>
);
}
Implementation Notes
Update Profile
- Updates only
nombre and apellidos fields
- Password cannot be changed through this endpoint
- No validation that the user exists (will succeed even if email doesn’t exist)
- Database connection errors raise HTTP 500
Get User Details
- Returns HTTP 404 if user not found
- Email parameter is case-sensitive
- Returns plain object (not nested in
data field)
- Database connection properly closed in
finally block
The update profile endpoint does not verify that the user exists before attempting the update. Consider adding existence validation in production.