Building a REST API

This lecture provides an overview for RESTful clients and how to build RESTful API for web services.

What is a Web Service?

Types of Web Services

SOAP-based
  • Traditional web services are implemented with a complicated architecture when the messages being passed is XML-based (eXtensible Markup Language).
  • It uses a Simple Object Accessing Protocol (SOAP) to provide the API for sending and receiving messages in the payload of an HTTP message.
JSON-based
  • The modern Web Services are using JavaScript Object Notation (JSON)format to structure the messages.
  • A JSON message stores the Get-Put-Update-Delete methods in the HTTP header in a request message; and a set of property-value pairs of data in the reply message.
  • It simplifies the protocol and the message structure for representational state (REST) web services.

What is a REST API?

REST
  • Representational State Transfer.
  • Is an architectural style for building distributed systems (especially on the web), defined by a set of constraints tha tend to produce systems that are scalable, performant, and evolvable.
Key Idea

Data and functionality are modeled as resources. Each resource is identified by a URI (often a URL on the web). Clients interact with resources by exchanging representations of those resources (commonly JSON today, but not required).

Core REST constraints
  • Client–server separation: Separate UI concerns vs. data concerns
  • Stateless requests: Each request contains what the server needs to understand it and the server doesn’t rely on stored conversation state.
  • Cacheable responses (when appropriate)
  • Uniform interface: A consistent way to interact with resources
  • Layered system: proxies, gateways, load balancers can sit in-between
Uniform interface
  • Resource identification via URI
  • Manipulating resources through representations
  • Self-descriptive messages (e.g., Content-Type, caching headers, auth context, etc.)
  • Hypermedia-driven navigation (clients discover valid next actions via links in responses)
HTTP methods in modern practice

HTTP methods have standardized semantics that REST APIs typically lean on:

  • GET: retrieve a representation (should be read-only; “safe”)
  • PUT: create or replace the representation at a known URI
  • DELETE: remove a resource
  • POST: submit data to a resource (often to create a subordinate resource or trigger processing);
  • PATCH (commonly used today): partial update
Additional notes
  • REST’s stateless constraint still allows for authentication via cookies, bearer tokens, etc. as long as each request carries the needed context.
  • Many production REST APIs are really HTTP+JSON APIs.

Developing a REST API

When we develop a REST API, we need to do the following:

  1. Develop a RESTful web service server (an endpoint),
  2. Develop RESTful web services API functions to implement the CRUD operations,
  3. Complete routers.
  4. Develop an API for a client to consume a web service. We need to develop EJS views to display the data received from the Endpoint.

The textbook’s lesson 26 will provide details on how to develop a Web Service Server.

Accessing the API

1
npm install bcrypt@latest