You’re building the blogging app and want to fetch all posts tagged “coding.” You write this code:
async function fetchPostsByTag(tag: string): Promise<Post[]> { const response = await fetch(`https://myblog.com/api/posts?tag=${tag}`); if (!response.ok) throw new Error(`Failed to fetch ${tag} posts`); const posts: Post[] = await response.json(); return posts; }
What does the ?tag=${tag} part do in the URL?
?tag=${tag}
It filters the posts returned by the server to match the tag
It updates an existing post’s tag
It specifies the HTTP method to use
It tells the server to create a new post with that tag
It deletes posts with that tag
A user clicks “Delete Draft” in the blogging app, triggering this code:
async function deletePost(postId: number): Promise<{ message: string }> { const response = await fetch(`https://myblog.com/api/posts/${postId}`, { method: "DELETE" }); if (!response.ok) throw new Error("Failed to delete post"); const result = await response.json(); return result; }
What happens if the server returns a 404 status code?
The server creates a new post instead
The browser retries the request automatically
The result is an empty array
result
The post is deleted successfully
The function throws an error
In the blogging app, you send a POST request to create a new post:
const newPost: Post = { id: 0, title: "My API Journey", tag: "coding" }; fetch("https://myblog.com/api/posts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(newPost), });
What should the server return if the post is created successfully?
The full list of all posts
A 200 status code with an empty response
A 404 status code to indicate a new resource
A 500 status code with an error message
A 201 status code with the new post’s ID
The blogging app fetches posts with fetch("https://myblog.com/api/posts"). The server doesn’t remember the user’s last request. Why is this a feature, not a bug?
fetch("https://myblog.com/api/posts")
It prevents the server from handling multiple users
It makes the API incompatible with fetch
fetch
It reduces server memory usage and improves scalability
It ensures the client sends less data in each request
It forces the client to store all data locally
You update a post’s title using this PATCH request:
const updates: Partial<Post> = { title: "Learning REST – Updated" }; fetch("https://myblog.com/api/posts/1", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(updates), });
Why is PATCH better than PUT here?
PUT is only for deleting resources
PATCH sends only the changed fields, not the whole post
PATCH returns a different status code
PATCH automatically retries failed requests
PUT requires authentication, PATCH doesn’t
A user reports that fetching posts (fetch("/api/posts")) returns too much data—titles, tags, content, and more—slowing the app. How could you fix this with REST?
fetch("/api/posts")
Delete unused posts from the server
Add a query parameter like ?fields=title,tag
?fields=title,tag
Make the server stateful to cache results
Use fetch with a smaller body payload
body
Switch to POST instead of GET
You’re debugging this code in the blogging app:
async function fetchAllPosts(): Promise<Post[]> { const response = await fetch("https://myblog.com/api/posts"); const posts: Post[] = await response.json(); return posts; }
The app crashes with “Failed to fetch posts.” What’s missing?
Error handling with if (!response.ok)
if (!response.ok)
A Content-Type header
Content-Type
A body with JSON data
Authentication credentials
A method: "GET" option in fetch
method: "GET"
The blogging app needs to fetch posts and their comments. Calling fetch("/api/posts/1") and fetch("/api/posts/1/comments") feels slow. What’s a RESTful solution?
fetch("/api/posts/1")
fetch("/api/posts/1/comments")
Fetch comments first, then posts
Make the server stateful to bundle the data
Use POST to fetch both at once
Combine them into fetch("/api/posts/1?include=comments")
fetch("/api/posts/1?include=comments")
Switch to a non-REST API
You write this endpoint for the blogging app: fetch("/api/getPosts"). Why is this a bad idea according to REST best practices?
fetch("/api/getPosts")
It’s not compatible with fetch
It’s too short and unclear
It should use DELETE instead of GET
It lacks query parameters
“get” is redundant since GET is implied by the method
A user authenticates in the blogging app, and every fetch request includes:
fetch(url, { headers: { Authorization: "Bearer token" } });
Why is this necessary given REST’s statelessness?
Each request must include all context, like auth, since the server forgets
The server stores the token between requests
It’s only needed for POST requests
It replaces query parameters
It prevents the server from scaling
This content is only available to registered users.Please register at the the home page.