Bird
0
0

You want to design a HAL response for a blog post that includes links to the author and comments. Which JSON structure correctly follows HAL format?

hard📝 Application Q15 of 15
Rest API - HATEOAS and Linking
You want to design a HAL response for a blog post that includes links to the author and comments. Which JSON structure correctly follows HAL format?
A{ "title": "My Post", "_links": { "self": { "href": "/posts/1" }, "author": { "href": "/users/42" }, "comments": [ { "href": "/comments/100" }, { "href": "/comments/101" } ] } }
B{ "title": "My Post", "links": { "self": "/posts/1", "author": "/users/42", "comments": ["/comments/100", "/comments/101"] } }
C{ "title": "My Post", "_links": { "self": "/posts/1", "author": "/users/42", "comments": "/comments/100,/comments/101" } }
D{ "title": "My Post", "_link": { "self": { "href": "/posts/1" }, "author": { "href": "/users/42" }, "comments": [ { "href": "/comments/100" }, { "href": "/comments/101" } ] } }
Step-by-Step Solution
Solution:
  1. Step 1: Verify the _links key and link objects

    { "title": "My Post", "_links": { "self": { "href": "/posts/1" }, "author": { "href": "/users/42" }, "comments": [ { "href": "/comments/100" }, { "href": "/comments/101" } ] } } correctly uses _links with each link as an object containing href. Multiple comments are an array of link objects.
  2. Step 2: Check other options for errors

    { "title": "My Post", "links": { "self": "/posts/1", "author": "/users/42", "comments": ["/comments/100", "/comments/101"] } } uses "links" without underscore and strings instead of objects. { "title": "My Post", "_links": { "self": "/posts/1", "author": "/users/42", "comments": "/comments/100,/comments/101" } } uses strings instead of objects for links. { "title": "My Post", "_link": { "self": { "href": "/posts/1" }, "author": { "href": "/users/42" }, "comments": [ { "href": "/comments/100" }, { "href": "/comments/101" } ] } } uses incorrect key "_link" instead of "_links".
  3. Final Answer:

    Option A JSON structure correctly follows HAL format -> Option A
  4. Quick Check:

    HAL requires _links with objects having href [OK]
Quick Trick: Use _links with objects and href for all links [OK]
Common Mistakes:
  • Missing underscore in _links
  • Using strings instead of objects for links
  • Wrong key name like _link

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes