Using JSONPlaceholder - a free fake REST API for testing.
Fetches a list of users from the API.
async function fetchUsers() {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await response.json();
console.log(users);
}
async function createPost(title, body) {
const response = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, body, userId: 1 })
});
const result = await response.json();
console.log('Created:', result);
}
Try fetching from invalid endpoints to see error handling: