Apis
This commit is contained in:
95
API Exercises Starter Template.html
Normal file
95
API Exercises Starter Template.html
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<!-- saved from url=(0063)file:///Users/home/Downloads/deepseek_html_20251215_dacfff.html -->
|
||||||
|
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||||
|
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>API Exercises Starter Template</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>API Exercises</h1>
|
||||||
|
|
||||||
|
<h2>Task 1: GET All Users (READ)</h2>
|
||||||
|
<button onclick="
|
||||||
|
fetch('https://api.techshare.cc/api/users')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
console.log('First 3 users:', data.slice(0, 3));
|
||||||
|
alert('Check console for first 3 users!');
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error:', error));
|
||||||
|
">
|
||||||
|
Get Users
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<h2>Task 2: GET Single User (READ)</h2>
|
||||||
|
<button onclick="
|
||||||
|
fetch('https://api.techshare.cc/api/users/693c0a4357cce1118095f635')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
console.log('Single user:', data);
|
||||||
|
alert('Check console for single user data!');
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error:', error));
|
||||||
|
">
|
||||||
|
Get User by _id
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<h2>Task 3: GET All Posts (READ)</h2>
|
||||||
|
<button onclick="
|
||||||
|
fetch('https://api.techshare.cc/api/posts')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
const titles = data.map(post => post.title);
|
||||||
|
console.log('First 5 titles:', titles.slice(0, 5));
|
||||||
|
alert('Check console for first 5 post titles!');
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error:', error));
|
||||||
|
">
|
||||||
|
Get Posts (Titles Only)
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<h2>Task 4: CREATE a Post (CREATE)</h2>
|
||||||
|
<button onclick="
|
||||||
|
fetch('https://api.techshare.cc/api/posts', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
title: 'My First API Post',
|
||||||
|
content: 'Learning APIs is fun!',
|
||||||
|
userId: 1,
|
||||||
|
published: true
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
console.log('New post created:', data);
|
||||||
|
alert('Post created! Check console for details.');
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error:', error));
|
||||||
|
">
|
||||||
|
Create Post
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<h2>Bonus: Filter Posts by User (READ)</h2>
|
||||||
|
<button onclick="
|
||||||
|
fetch('https://api.techshare.cc/api/posts')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(posts => {
|
||||||
|
const userPosts = posts.filter(post => post.userId === 1);
|
||||||
|
console.log('Posts by user 1:', userPosts);
|
||||||
|
alert('Check console for posts by user 1!');
|
||||||
|
})
|
||||||
|
.catch(error => console.error('Error:', error));
|
||||||
|
">
|
||||||
|
Filter Posts by User
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Optional: You could move the functions here for cleaner separation
|
||||||
|
// But keeping them in onclick handlers as requested
|
||||||
|
console.log('API Exercises Template Ready!');
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body></html>
|
||||||
Reference in New Issue
Block a user