Pie Logo
Quiz: REST APIs
by Tomas Trescak

QuizTest your understanding of the section content.

ContentDetailed description of the section content.

Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
Next.js
Advanced React
Databases
React Hooks
Authentication and Authorisation
APIs
APIs
Rest APIs
Rest APIs - Express.js
Rest APIs - Next.js
Quiz: REST APIs
tRPC
Securing APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: REST APIs
    by Tomas Trescak

    QuizTest your understanding of the section content.

    ContentDetailed description of the section content.

    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    APIs
    Rest APIs
    Rest APIs - Express.js
    Rest APIs - Next.js
    Quiz: REST APIs
    tRPC
    Securing APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    QUESTION 1

    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?


    QUESTION 2

    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?


    QUESTION 3

    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?


    QUESTION 4

    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?


    QUESTION 5

    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?


    QUESTION 6

    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?


    QUESTION 7

    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?


    QUESTION 8

    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?


    QUESTION 9

    You write this endpoint for the blogging app: fetch("/api/getPosts"). Why is this a bad idea according to REST best practices?


    QUESTION 10

    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?

    This content is only available to registered users.
    Please register at the the home page.

    QUESTION 1

    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?


    QUESTION 2

    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?


    QUESTION 3

    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?


    QUESTION 4

    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?


    QUESTION 5

    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?


    QUESTION 6

    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?


    QUESTION 7

    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?


    QUESTION 8

    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?


    QUESTION 9

    You write this endpoint for the blogging app: fetch("/api/getPosts"). Why is this a bad idea according to REST best practices?


    QUESTION 10

    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?

    This content is only available to registered users.
    Please register at the the home page.

    Pie Logo
    Quiz: REST APIs
    by Tomas Trescak
    Pie Logo
    Quiz: REST APIs
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    APIs
    Rest APIs
    Rest APIs - Express.js
    Rest APIs - Next.js
    Quiz: REST APIs
    tRPC
    Securing APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    APIs
    Rest APIs
    Rest APIs - Express.js
    Rest APIs - Next.js
    Quiz: REST APIs
    tRPC
    Securing APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics