Building a REST service

Designing a REST service: resources and URIs, the meaning of each HTTP method, response codes, statelessness, safe and idempotent methods.

  • 29 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Resource (REST): what is it?

    Show the answer

    A thing the service exposes and that a URI identifies: a customer, an order, the list of a customer's orders.

    The URI names the thing (/clients/7/orders/42), the HTTP method says the action. A verb in the URI (/deleteClient) mixes the two.

  • Which request creates an order for customer 7 in a well-designed REST service?

    • POST /clients/7/commandes
    • GET /clients/7/commandes/nouvelle
    • PUT /clients/7/creerCommande
    • POST /clients/7/commandes/creer
    Show the answer

    POST /clients/7/commandes

    The URI points to the collection (a plural noun) and POST says “add an item to it”. A verb in the URI (creer, creerCommande) or a GET that writes breaks the convention.

  • In a REST service, … reads a resource, … creates one, … replaces it entirely and … removes it.

    Show the answer

    In a REST service, GET reads a resource, POST creates one, PUT replaces it entirely and DELETE removes it.

    PATCH completes the list: it changes only the fields sent, where PUT replaces the whole resource.

  • Stateless service: what is it?

    Show the answer

    A service where each request carries everything needed to process it: the server keeps nothing about the client from one request to the next.

    Any server can answer any request, which allows load to be spread. The authentication token therefore travels with every request.

  • A crawler walks the site and a customer vanishes from the database: a route GET /deleteClient?id=3 exists. Which fix follows the REST convention?

    • Replace it with POST /clients/3 with the word delete in the body
    • Replace it with DELETE /clients/3
    • Require the crawler to add the Accept: application/json header
    • Forbid the route to crawlers in the robots.txt file
    Show the answer

    Replace it with DELETE /clients/3

    Crawlers, caches and browsers follow GET freely because it is safe. robots.txt would only keep out polite crawlers, not a browser prefetching a link. A deletion must go through a method that does not fire by itself: DELETE.