Application Protocols · Study deck

REST API Practice: Protocol Review

A smart thermostat needs a web address, or URL, for its reading.

Broker Bex is your guide for this deck.

rest
Broker Bex, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Test quiz 2: comprehensive review with a concrete scenario and pass criteria.
  • Define hands-on exercise: design a smart parking rest api with explicit inputs, errors, and change rules.
  • Explain: Scenario: Your IoT REST API receives a request for device data, but the device is offline.
  • Explain: Mobile apps can display "Last updated 1 hour ago" without treating it as an error.
iotclass.org

Major section

Decision Framework: When to Return 404 vs 503 vs 200 with Stale Data

Scenario: Your IoT REST API receives a request for device data, but the device is offline.

  • The API request was successful -- we returned valid temperature data.
  • The client can use the connectivity.status field to decide how to handle staleness.
  • Device is online but unresponsive (hung firmware).
iotclass.org

Major section

Decision Framework: When to Return 404 vs 503 vs 200 with Stale Data (continued)

Mobile apps can display "Last updated 1 hour ago" without treating it as an error.

  • Offline devices are expected in IoT -- not an error condition.
  • No cached data available and request requires live data.
  • Cloud gateway can't reach device network (network partition).
  • Key principle: IoT APIs should distinguish between "resource doesn't exist" (404), "resource exists but unavailable" (503), and "resource available but data is stale" (200 with metadata).
iotclass.org

Major section

Concept Relationships

Resource identity decides when to use 404.

  • Offline device handling requires distinguishing between no data (404) and stale data (200 with metadata), a pattern that extends to all IoT REST APIs.

Resource identity decides when to use 404. Link cost and gateway roles guide protocol choice.

iotclass.org

Major section

Deep Dive: Reading an HTTP Message Like an Engineer

A REST response is a control signal for firmware, not just a message for a dashboard.

  • For example, 202 Accepted fits a command queued for a sleeping device, while 204 No Content would falsely imply the action already completed.
  • Authorization proves who is calling before anything is created.

Key terms

Location
Location is the authoritative URL for the new resource; the device should persist /v1/devices/42 instead of guessing its assigned identifier.
iotclass.org

Major section

Deep Dive: Reading an HTTP Message Like an Engineer (continued)

Returning 500 for a bad client payload makes devices retry a request that can never succeed.

  • Content-Length must match the actual byte count so parsers can detect truncation or smuggling mistakes.
  • The two fleet-scale traps are status masking and retry amplification.
  • That line is the API contract devices actually execute.
iotclass.org

Deck summary

Key takeaways

Scenario: Your IoT REST API receives a request for device data, but the device is offline.

  • Mobile apps can display "Last updated 1 hour ago" without treating it as an error.
  • Resource identity decides when to use 404.
  • A REST response is a control signal for firmware, not just a message for a dashboard.
  • Returning 500 for a bad client payload makes devices retry a request that can never succeed.
iotclass.org

Retrieval practice

Recall check 1 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q1A soil sensor wakes every six hours and sends a 15-byte reading. In the chapter’s illustrative battery model, CoAP uses 0.0347 mAh/day and MQTT uses 0.0438 mAh/day with a fresh TCP connection each wake. Comparing these modeled costs, which protocol saves energy and by how much?

ACoAP uses ~21% less energy (~26% longer life) via UDP without TCP setup.
BMQTT saves 30% energy because TCP reliability avoids retransmissions
CMQTT and CoAP use equal energy because both are lightweight IoT protocols
DHTTP over TCP saves the most energy through its mature implementation
Show answer

Answer: A see answers page

iotclass.org

Retrieval practice

Recall check 2 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q2A smart home system has a phone app that needs to directly query the current temperature from a thermostat and adjust its setpoint. The system also has a cloud dashboard subscribed to temperature updates from 5 thermostats in different rooms for historical logging. Which protocol architecture provides the most efficient solution?

AMQTT for local phone control and cloud logging, with the phone depending on a broker
BCoAP GET/PUT and Observe for every thermostat and cloud subscription
CCoAP GET/PUT for direct phone control; MQTT topics for cloud dashboard updates
DHTTP REST for local phone control; MQTT topics for cloud dashboard updates
Show answer

Answer: C Match the pattern: direct request/response for local control with CoAP GET/PUT; pub/sub fan-out for the dashboard with MQTT.

iotclass.org

Retrieval practice

Recall check 3 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q3A factory sends direct one-shot alerts from 200 pressure sensors over a link with 5% independent packet loss. Which configuration provides acknowledgment and retry for the alert path? A separate safety path must still meet the <100 ms action deadline under loss.

ACoAP NON alerts without acknowledgment or retry after packet loss
BMQTT QoS 2 alerts with a broker hop and exactly-once handshake
CCoAP CON alerts with acknowledgment and retransmission for a direct path
DHTTP polling every 50 ms by 200 sensors for each new alert
Show answer

Answer: C CoAP CON supplies acknowledgment and retransmission for direct messages.

iotclass.org

Retrieval practice

Recall check 4 of 4

Broker Bex says: answer from memory, then check your reasoning.

Q4For the chapter’s minimal-header Ethernet/IPv4 example, a 4-byte payload ("25.3") travels with 4-byte CoAP, 8-byte UDP, 20-byte IPv4 and 14-byte Ethernet headers, with no options or security. What percentage of the 50-byte frame is overhead?

A25% - CoAP is very efficient
B50% - Half protocol, half payload
C10% - Only CoAP header counts
D92% protocol overhead.
Show answer

Answer: D The minimal Ethernet/IPv4 model counts 46 header bytes in 50 total bytes: 92%.

iotclass.org

Print reference

Answers 1 of 2

Answer key.

  1. A · The chapter’s illustrative daily figures give CoAP 0.0347 mAh/day and MQTT 0.0438 mAh/day: (0.0438 − 0.0347) / 0.0438 = 20.8% less modeled energy for CoAP. This is a scenario calculation, not a protocol guarantee or a two-year battery-life prediction. Measure the complete secured connection, retransmissions, radio tails, and sleep current.
  2. C · Match the pattern: direct request/response for local control with CoAP GET/PUT; pub/sub fan-out for the dashboard with MQTT.
iotclass.org

Print reference

Answers 2 of 2

Answer key.

  1. C · CoAP CON supplies acknowledgment and retransmission for direct messages.
  2. D · The minimal Ethernet/IPv4 model counts 46 header bytes in 50 total bytes: 92%.
iotclass.org