Bird
Raised Fist0
Rest APIprogramming~20 mins

Action links for state transitions 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
🎖️
Action Link 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 REST API response with action links?

Given this JSON response from a REST API representing an order, what is the value of the href for the cancel action link?

Rest API
{
  "orderId": "12345",
  "status": "processing",
  "actions": {
    "cancel": {
      "href": "/orders/12345/cancel",
      "method": "POST"
    },
    "track": {
      "href": "/orders/12345/track",
      "method": "GET"
    }
  }
}
A"/orders/12345/track"
B"/orders/cancel/12345"
C"/orders/12345/cancel"
D"/cancel/orders/12345"
Attempts:
2 left
💡 Hint

Look for the cancel key inside actions and check its href value.

🧠 Conceptual
intermediate
1:30remaining
Which HTTP method is typically used for state transition actions like 'approve' or 'reject' in REST APIs?

In REST APIs, when you provide action links for state transitions such as 'approve' or 'reject' an item, which HTTP method is most appropriate to use?

APUT
BPOST
CDELETE
DGET
Attempts:
2 left
💡 Hint

Think about methods that cause a change in server state but are not idempotent.

🔧 Debug
advanced
2:00remaining
Identify the error in this action link JSON for a state transition

What error will occur when a client tries to use this action link to transition the state?

Rest API
{
  "actions": {
    "ship": {
      "href": "/orders/789/ship",
      "method": "GET"
    }
  }
}
A404 Not Found error because the URL is invalid
BNo error; the action link is valid
CSyntaxError due to missing comma
DUsing GET method for a state-changing action causes unexpected behavior
Attempts:
2 left
💡 Hint

Consider HTTP method semantics for state changes.

📝 Syntax
advanced
1:30remaining
Which option correctly represents an action link for a 'complete' state transition in JSON?

Choose the syntactically correct JSON snippet for an action link named 'complete' that uses POST method.

A{ "complete": { "href": "/tasks/456/complete", "method": "POST" } }
B{ complete: { href: "/tasks/456/complete", method: "POST" } }
C{ "complete": { "href": "/tasks/456/complete", "method": POST } }
D{ "complete": { "href": "/tasks/456/complete", method: "POST" }
Attempts:
2 left
💡 Hint

Remember JSON requires double quotes around keys and string values.

🚀 Application
expert
2:30remaining
How many action links are available for this resource and what are their HTTP methods?

Given this JSON snippet for a resource, count the number of action links and list their HTTP methods.

Rest API
{
  "id": "555",
  "status": "pending",
  "actions": {
    "approve": { "href": "/requests/555/approve", "method": "POST" },
    "reject": { "href": "/requests/555/reject", "method": "POST" },
    "cancel": { "href": "/requests/555/cancel", "method": "DELETE" }
  }
}
A3 action links: POST, POST, DELETE
B2 action links: POST, DELETE
C4 action links: POST, POST, DELETE, GET
D3 action links: GET, POST, DELETE
Attempts:
2 left
💡 Hint

Count all keys inside the actions object and note their methods.

Practice

(1/5)
1. What is the main purpose of action links in REST APIs for state transitions?
easy
A. To format the API response as JSON
B. To store data permanently on the server
C. To authenticate users before accessing the API
D. To provide URLs that clients can use to change the current state

Solution

  1. Step 1: Understand what action links represent

    Action links are URLs included in API responses that show possible next steps or actions a client can take to change the state.
  2. Step 2: Identify the purpose of action links

    They guide clients on how to move from one state to another by calling these URLs.
  3. Final Answer:

    To provide URLs that clients can use to change the current state -> Option D
  4. Quick Check:

    Action links = URLs for state change [OK]
Hint: Action links = URLs for next steps in state [OK]
Common Mistakes:
  • Confusing action links with authentication tokens
  • Thinking action links store data
  • Assuming action links format data
2. Which of the following is the correct way to include an action link for a "cancel" operation in a REST API JSON response?
easy
A. "cancel": "POST https://api.example.com/orders/123/cancel"
B. "actions": {"cancel": "https://api.example.com/orders/123/cancel"}
C. "cancel_url": "https://api.example.com/orders/123/cancel"
D. "cancel_link": "GET https://api.example.com/orders/123/cancel"

Solution

  1. Step 1: Recognize common pattern for action links

    Action links are often grouped under an "actions" key with action names as keys and URLs as values.
  2. Step 2: Check each option's format

    "actions": {"cancel": "https://api.example.com/orders/123/cancel"} correctly uses an "actions" object with "cancel" as key and the URL as value, which is a clear and common pattern.
  3. Final Answer:

    "actions": {"cancel": "https://api.example.com/orders/123/cancel"} -> Option B
  4. Quick Check:

    Action links grouped under "actions" = "actions": {"cancel": "https://api.example.com/orders/123/cancel"} [OK]
Hint: Group action links under "actions" key for clarity [OK]
Common Mistakes:
  • Using HTTP method inside the URL string
  • Not grouping actions under a common key
  • Using incorrect HTTP method for cancel
3. Given this JSON response snippet from a REST API:
{
  "state": "pending",
  "actions": {
    "approve": "https://api.example.com/items/42/approve",
    "reject": "https://api.example.com/items/42/reject"
  }
}

What will happen if the client calls the URL in the "approve" action link?
medium
A. The item state will change to approved
B. The item will be deleted
C. The client will receive an error because the URL is invalid
D. The item state will remain pending

Solution

  1. Step 1: Understand the meaning of the "approve" action link

    The "approve" link is provided as a next step to change the state from "pending" to "approved" by calling that URL.
  2. Step 2: Predict the effect of calling the approve URL

    Calling the approve URL triggers the state transition to "approved" as intended by the API design.
  3. Final Answer:

    The item state will change to approved -> Option A
  4. Quick Check:

    Calling "approve" URL = state changes to approved [OK]
Hint: Action link name hints the state change [OK]
Common Mistakes:
  • Assuming the URL deletes the item
  • Thinking the URL is invalid
  • Believing state stays the same after action
4. A REST API response includes this action link:
"actions": {"complete": "https://api.example.com/tasks/99/complete"}

But calling this URL returns a 405 Method Not Allowed error. What is the most likely cause?
medium
A. The task with ID 99 does not exist
B. The URL is misspelled in the response
C. The client used GET instead of POST to call the action link
D. The server is down

Solution

  1. Step 1: Understand 405 Method Not Allowed error

    This error means the HTTP method used is not supported by the URL endpoint.
  2. Step 2: Identify common cause with action links

    Action links for state changes usually require POST, but clients often call them with GET by mistake.
  3. Final Answer:

    The client used GET instead of POST to call the action link -> Option C
  4. Quick Check:

    405 error = wrong HTTP method used [OK]
Hint: Use POST for action links, not GET [OK]
Common Mistakes:
  • Assuming URL is misspelled without checking
  • Thinking 405 means resource missing
  • Blaming server downtime without evidence
5. You want to design a REST API for an order system with states: new, paid, shipped, and cancelled. Which of the following JSON responses best uses action links to guide clients through valid state transitions when the order is in paid state?
hard
A. { "state": "paid", "actions": { "ship": "https://api.example.com/orders/555/ship", "cancel": "https://api.example.com/orders/555/cancel" } }
B. { "state": "paid", "actions": { "pay": "https://api.example.com/orders/555/pay", "cancel": "https://api.example.com/orders/555/cancel" } }
C. { "state": "paid", "actions": { "new": "https://api.example.com/orders/555/new", "cancel": "https://api.example.com/orders/555/cancel" } }
D. { "state": "paid", "actions": { "ship": "https://api.example.com/orders/555/ship", "pay": "https://api.example.com/orders/555/pay" } }

Solution

  1. Step 1: Identify valid next states from "paid"

    From "paid", the order can be "shipped" or "cancelled" but not "pay" or "new" again.
  2. Step 2: Check which options provide correct action links

    { "state": "paid", "actions": { "ship": "https://api.example.com/orders/555/ship", "cancel": "https://api.example.com/orders/555/cancel" } } correctly offers "ship" and "cancel" actions, matching valid transitions.
  3. Final Answer:

    JSON with "ship" and "cancel" actions for "paid" state -> Option A
  4. Quick Check:

    Valid next actions for "paid" = ship, cancel [OK]
Hint: Only include valid next states as action links [OK]
Common Mistakes:
  • Including actions that repeat previous states
  • Missing valid next state actions
  • Confusing state names with action names