Bird
0
0

Which JSON structure correctly uses link relations to represent these?

hard📝 Application Q15 of 15
Rest API - HATEOAS and Linking
You want to design a REST API response for a blog post that includes links to the post itself, the author's profile, and comments. Which JSON structure correctly uses link relations to represent these?
A{ "links": [ { "rel": "self", "url": "/posts/10" }, { "rel": "author", "url": "/users/5" }, { "rel": "comments", "url": "/posts/10/comments" } ] }
B{ "_links": { "self": { "href": "/posts/10" }, "author": { "href": "/users/5" }, "comments": { "href": "/posts/10/comments" } } }
C{ "_links": { "self": "/posts/10", "author": "/users/5", "comments": "/posts/10/comments" } }
D{ "_links": { "self": { "url": "/posts/10" }, "author": { "url": "/users/5" }, "comments": { "url": "/posts/10/comments" } } }
Step-by-Step Solution
Solution:
  1. Step 1: Recall the correct link relation format

    Each link relation should be an object with an 'href' key inside the '_links' object.
  2. Step 2: Evaluate each option's structure

    { "_links": { "self": { "href": "/posts/10" }, "author": { "href": "/users/5" }, "comments": { "href": "/posts/10/comments" } } } correctly uses '_links' with 'self', 'author', and 'comments' keys, each having an 'href' URL. Options B and D use 'url' instead of 'href', and C uses strings instead of objects.
  3. Final Answer:

    { "_links": { "self": { "href": "/posts/10" }, "author": { "href": "/users/5" }, "comments": { "href": "/posts/10/comments" } } } -> Option B
  4. Quick Check:

    Use '_links' with objects containing 'href' URLs [OK]
Quick Trick: Use '_links' with 'href' keys for each relation [OK]
Common Mistakes:
  • Using 'url' instead of 'href' for links
  • Using arrays instead of objects for link relations
  • Assigning string URLs directly without 'href' objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes