Bird
Raised Fist0

You are designing a REST API response for a blog post that must include related links to author and comments. Which JSON structure correctly represents these links following REST best practices?

hard🚀 Application Q8 of Q15
Rest API - HATEOAS and Linking
You are designing a REST API response for a blog post that must include related links to author and comments. Which JSON structure correctly represents these links following REST best practices?
A{ "post": { "id": 101, "title": "REST API Design", "links": { "author": {"href": "/authors/12"}, "comments": {"href": "/posts/101/comments"} } } }
B{ "post": { "id": 101, "title": "REST API Design", "author_link": "/authors/12", "comments_link": "/posts/101/comments" } }
C{ "post": { "id": 101, "title": "REST API Design", "related": [ "/authors/12", "/posts/101/comments" ] } }
D{ "post": { "id": 101, "title": "REST API Design", "links": [ {"author": "/authors/12"}, {"comments": "/posts/101/comments"} ] } }
Step-by-Step Solution
Solution:
  1. Step 1: Identify best practice for related links

    Related resource links should be grouped under a links object with named keys and href values.
  2. Step 2: Analyze each option

    { "post": { "id": 101, "title": "REST API Design", "links": { "author": {"href": "/authors/12"}, "comments": {"href": "/posts/101/comments"} } } } correctly nests links under "links" with href objects. { "post": { "id": 101, "title": "REST API Design", "author_link": "/authors/12", "comments_link": "/posts/101/comments" } } uses separate keys without grouping. { "post": { "id": 101, "title": "REST API Design", "related": [ "/authors/12", "/posts/101/comments" ] } } uses an array of URLs without keys, losing clarity. { "post": { "id": 101, "title": "REST API Design", "links": [ {"author": "/authors/12"}, {"comments": "/posts/101/comments"} ] } } uses an array of objects but keys are inside objects, which is less standard.
  3. Final Answer:

    { "post": { "id": 101, "title": "REST API Design", "links": { "author": {"href": "/authors/12"}, "comments": {"href": "/posts/101/comments"} } } } provides the clearest, most RESTful structure.
  4. Quick Check:

    Links grouped under "links" with href objects is standard [OK]
Quick Trick: Group related links under a 'links' object with href keys [OK]
Common Mistakes:
MISTAKES
  • Using arrays without keys for related links
  • Not grouping links under a dedicated object
  • Using separate keys instead of a links container

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes