The answersDownload the PDF
Worksheet

A12.8 Client server, REST and thin clients

Networks and the web · A level · OCR H446 1.3.3, AQA 7517 4.9.2.2, Eduqas A500QS 2.5 · about 30 min

BugBotLab
NameClassDate

What this lesson is about

Peer to peer and client server, websockets, CRUD and REST with JSON, and thin versus thick clients.

Questions 6 marks in all

  1. [1 mark]Which HTTP method and SQL statement match the CRUD operation Update?

    1. APUT and UPDATE
    2. BPOST and INSERT
    3. CGET and SELECT
    4. DDELETE and DROP
  2. [1 mark]How does the websocket protocol differ from ordinary HTTP requests?

    1. AIt keeps a persistent full-duplex connection, so the server can push data to the client at any time
    2. BIt encrypts every message with a digital certificate
    3. CIt only allows the client to send data
    4. DIt uses UDP instead of TCP
  3. [1 mark]Why is JSON often preferred to XML for web APIs? Choose all that apply.

    Tick every answer that is true.

    1. AIt is more compact
    2. BIt is easier and quicker for a computer to parse
    3. CIt is easier for a human to read
    4. DIt supports schemas that validate structure better
  4. [1 mark]A REST API receives DELETE /robots/7, but there is no robot 7. Which status code should it return?

    1. A404
    2. B200
    3. C201
    4. D204
  5. [1 mark]Which are advantages of thin-client computing? Choose all that apply.

    Tick every answer that is true.

    1. AClient devices can be cheap
    2. BSoftware and data are managed centrally
    3. CClients keep working without a network connection
    4. DThe server carries less load
  6. [1 mark]Which is a disadvantage of a peer-to-peer network compared with client-server?

    1. AThere is no central control of security and backups
    2. BIt needs an expensive dedicated server
    3. CIf the server fails, every computer loses the service
    4. DPeers cannot share files

The task: the robot as a REST server

Turn the robot into a tiny REST server. The Client on this mat sends six requests over the radio, one a second. Each is words separated by single spaces: a method, a path, and for some an extra value. The robot has two kinds of resource: /led, its LED colour (starting as off), and /moves/<id>, the moves it has made, each stored as its distance in whole centimetres with ids counting from 1. Handle each request like this: | Request | What to do | Send | |---|---|---| | GET /led | nothing | 200 <colour> | | PUT /led <colour> | set the LED to that colour name | 200 <colour> | | POST /moves <cm> | drive forward <cm> cm at speed 50, store it under the next id | 201 /moves/<id> | | GET /moves/<id> | nothing | 200 <cm>, or 404 if there is no such move | | DELETE /moves/<id> | remove the move | 204, or 404 if there is no such move | | any other path | nothing | 404 | After sending each response, print <request> -> <response>. Stop after the sixth request, so the output is 6 lines. Check messages() every 0.1 s, and build responses from your stored resources rather than typing them.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

colour = "off"
moves = {}
next_id = 1

wait(6)
for sender, text in messages():
    print(text)

Plan your program here, then type it in and press Run.

QR code
Do it on the robot
www.bugbotlab.com/learn/a12-8-client-server-and-rest/
The simulator checks it and tells you when it passes. Nothing to install, no account.

Challenges

  1. Send 405 for methods a resource does not support, such as DELETE /led.
  2. Return each move as JSON: 200 {"id": 1, "cm": 20}.
  3. Which of these would you build as a thin client and which as a thick one: a school's exam results system, a video editor, a supermarket till? Give a reason for each.