0
0
Rest APIprogramming~20 mins

Nested resources in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested Resource Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested resource URL handling

Consider a REST API where /users/{userId}/posts/{postId} returns a specific post of a user. What is the output of the following request handler when called with /users/5/posts/10?

Rest API
def get_post(user_id, post_id):
    return f"User {user_id} requested post {post_id}"
A"User 5 requested post 10"
B"User 10 requested post 5"
C"Post 10 of user 5 not found"
D"Error: Invalid user or post ID"
Attempts:
2 left
💡 Hint

Look carefully at the order of parameters in the function and the URL.

🧠 Conceptual
intermediate
1:30remaining
Understanding nested resource URL structure

Which of the following URLs correctly represents a nested resource where comments belong to a specific post?

A/comments/123/posts/456
B/posts/comments/123/456
C/posts/456/comments/123
D/posts/123/456/comments
Attempts:
2 left
💡 Hint

Think about the hierarchy: comments belong to posts, so posts come first in the URL.

🔧 Debug
advanced
2:30remaining
Identify the error in nested resource route definition

Given the following Flask route definitions, which option correctly identifies the error?

@app.route('/users//posts/')
def get_post(user_id, post_id):
    return f"User {user_id} post {post_id}"

@app.route('/users//posts')
def get_posts(user_id):
    return f"Posts for user {user_id}"

@app.route('/posts/')
def get_post_global(post_id):
    return f"Post {post_id} globally"
AThere is no error; all routes are correctly defined.
BThe function get_post is missing a return statement.
CThe route '/users/<int:user_id>/posts' should be defined after '/users/<int:user_id>/posts/<int:post_id>'.
DThe route '/posts/<int:post_id>' conflicts with '/users/<int:user_id>/posts/<int:post_id>' causing ambiguity.
Attempts:
2 left
💡 Hint

Consider how Flask matches routes and if any overlap can cause confusion.

📝 Syntax
advanced
2:00remaining
Correct syntax for nested resource route in Express.js

Which of the following Express.js route definitions correctly handles a nested resource for /users/:userId/posts/:postId?

Aapp.get('/users/:userId/posts/:postId', (res, req) => { res.send(`User ${req.params.userId} Post ${req.params.postId}`); });
Bapp.get('/users/:userId/posts/:postId', (req, res) => { res.send(`User ${req.params.userId} Post ${req.params.postId}`); });
Capp.get('/users/:userId/posts/:postId', (req, res) => { res.send('User ' + req.userId + ' Post ' + req.postId); });
Dapp.get('/users/:userId/posts/:postId', (req, res) => { res.send(`User ${req.params.postId} Post ${req.params.userId}`); });
Attempts:
2 left
💡 Hint

Remember the order of parameters in Express route handlers and how to access URL parameters.

🚀 Application
expert
1:30remaining
Number of items returned by nested resource query

In a REST API, the endpoint /users/7/orders returns all orders for user 7. The database has the following data:

  • User 7 has 3 orders: IDs 101, 102, 103
  • User 8 has 2 orders: IDs 201, 202

If the API code filters orders by user ID correctly, how many items will the response contain when calling /users/7/orders?

A5
B2
C0
D3
Attempts:
2 left
💡 Hint

Count only orders belonging to user 7.