Bird
Raised Fist0
Rest APIprogramming~20 mins

HAL format overview in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
HAL Format Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this HAL JSON snippet?
Given this HAL JSON snippet, what is the value of the 'self' link href?
Rest API
{
  "_links": {
    "self": { "href": "/orders/123" },
    "next": { "href": "/orders/124" },
    "find": { "href": "/orders{?id}", "templated": true }
  },
  "total": 1,
  "_embedded": {
    "orders": [
      { "id": 123, "status": "shipped" }
    ]
  }
}
A"/orders{?id}"
B"/orders/123"
C"/orders/124"
D"/orders/125"
Attempts:
2 left
💡 Hint
Look inside the _links object for the 'self' key.
🧠 Conceptual
intermediate
1:30remaining
What is the purpose of the _embedded section in HAL?
In HAL format, what does the _embedded section do?
AIt contains related resources embedded inside the current resource.
BIt lists all available HTTP methods.
CIt specifies the API version used.
DIt defines HTTP headers for the response.
Attempts:
2 left
💡 Hint
Think about how HAL helps clients get related data in one response.
🔧 Debug
advanced
2:30remaining
What error does this HAL JSON cause?
This HAL JSON snippet is invalid. What error will it cause when parsed?
Rest API
{
  "_links": {
    "self": { "href": "/users/1" },
    "friends": [
      { "href": "/users/2" },
      { "href": "/users/3" }
    ]
  },
  "name": "Alice",
  "age": 30
}
ANo error, valid HAL JSON
BTypeError because 'friends' should not be an array
CKeyError because '_embedded' is missing
DSyntaxError due to missing comma between 'name' and 'age'
Attempts:
2 left
💡 Hint
Check JSON syntax carefully between properties.
📝 Syntax
advanced
2:00remaining
Which option correctly represents a templated link in HAL?
Choose the correct HAL syntax for a templated link to search orders by id.
A"search": { "href": "/orders/{id}", "templated": true }
B"search": { "href": "/orders?id", "templated": false }
C"search": { "href": "/orders{?id}", "templated": true }
D"search": { "href": "/orders?id", "templated": true }
Attempts:
2 left
💡 Hint
Templated links use curly braces with question mark for optional query params.
🚀 Application
expert
1:30remaining
How many items are in the embedded orders list?
Given this HAL JSON, how many orders are embedded inside the _embedded section? { "_links": { "self": { "href": "/orders" } }, "_embedded": { "orders": [ { "id": 101, "status": "pending" }, { "id": 102, "status": "shipped" }, { "id": 103, "status": "delivered" } ] }, "count": 3 }
A3
B4
C0
D1
Attempts:
2 left
💡 Hint
Count the number of objects inside the orders array in _embedded.

Practice

(1/5)
1. What is the main purpose of the _links section in a HAL formatted REST API response?
easy
A. To define the API version number
B. To store user authentication tokens
C. To list all data fields in the response
D. To provide URLs to related resources for easy navigation

Solution

  1. Step 1: Understand the role of _links in HAL

    The _links section contains URLs pointing to related resources, helping clients navigate the API easily.
  2. Step 2: Compare options with HAL purpose

    Options B, C, and D do not describe navigation or linking related resources, which is the key feature of _links.
  3. Final Answer:

    To provide URLs to related resources for easy navigation -> Option D
  4. Quick Check:

    HAL _links = URLs for related resources [OK]
Hint: Remember: _links always holds related resource URLs [OK]
Common Mistakes:
  • Confusing _links with data fields
  • Thinking _links stores authentication info
  • Assuming _links defines API version
2. Which of the following is the correct way to represent a link to a 'self' resource in HAL JSON?
easy
A. "_links": { "self": { "href": "/orders/123" } }
B. "links": { "self": "/orders/123" }
C. "_links": { "self": "/orders/123" }
D. "_link": { "href": "/orders/123" }

Solution

  1. Step 1: Recall HAL syntax for links

    HAL requires a _links object with named links, each having an href property.
  2. Step 2: Check each option's structure

    "_links": { "self": { "href": "/orders/123" } } correctly uses _links with self containing an object with href. Others miss underscores, use wrong keys, or omit href.
  3. Final Answer:

    "_links": { "self": { "href": "/orders/123" } } -> Option A
  4. Quick Check:

    HAL link = _links + href [OK]
Hint: HAL links always use _links and href keys [OK]
Common Mistakes:
  • Omitting underscore in _links
  • Using string instead of object for link value
  • Missing href property inside link
3. Given the following HAL JSON snippet, what is the URL to access the customer's details?
{
  "_links": {
    "self": { "href": "/orders/123" },
    "customer": { "href": "/customers/456" }
  },
  "orderNumber": "123"
}
medium
A. /orders/123
B. /customers/456
C. /orders/456
D. /customers/123

Solution

  1. Step 1: Identify the customer link in _links

    The customer key has an href value of "/customers/456" which points to the customer's details URL.
  2. Step 2: Confirm other options

    self points to the order URL "/orders/123". Other options mix order and customer IDs incorrectly.
  3. Final Answer:

    /customers/456 -> Option B
  4. Quick Check:

    Customer link = _links.customer.href = /customers/456 [OK]
Hint: Look inside _links for the named resource URL [OK]
Common Mistakes:
  • Confusing self with customer link
  • Mixing order and customer IDs
  • Ignoring the href property
4. Identify the error in this HAL JSON snippet:
{
  "_links": {
    "self": "/orders/123",
    "items": [{ "href": "/items/1" }, { "href": "/items/2" }]
  }
}
medium
A. The JSON is correct as is
B. "items" should be a single object, not an array
C. "self" link should be an object with an "href" property
D. "_links" key should be "links" without underscore

Solution

  1. Step 1: Check the structure of the "self" link

    In HAL, each link must be an object with an href property. Here, "self" is a string, which is incorrect.
  2. Step 2: Verify other parts

    "items" is correctly an array of link objects. The _links key must have an underscore. So only "self" link is wrong.
  3. Final Answer:

    "self" link should be an object with an "href" property -> Option C
  4. Quick Check:

    HAL links = objects with href [OK]
Hint: All HAL links must be objects with href keys [OK]
Common Mistakes:
  • Using string instead of object for single links
  • Removing underscore from _links
  • Thinking arrays are not allowed for multiple links
5. 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
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" } ] } }

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]
Hint: 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