Junior — Middle
How can user information be updated via REST API?
sobes.tech AI
Answer from AI
To update user information via REST API, the HTTP methods PUT or PATCH are typically used.
PUTreplaces the entire user entity with new data.PATCHupdates only specified fields.
Example in Python using the requests library to update a user:
import requests
url = 'https://api.example.com/users/123'
headers = {'Authorization': 'Bearer your_token', 'Content-Type': 'application/json'}
data = {
'name': 'New Name',
'email': 'newemail@example.com'
}
response = requests.patch(url, json=data, headers=headers)
if response.status_code == 200:
print('User information successfully updated')
else:
print(f'Update error: {response.status_code} - {response.text}')
Important:
- Ensure you have permissions to update data.
- Use the correct URL and method as specified by the API.
- Send data in the format expected by the server (usually JSON).