Skip to content

Latest commit

 

History

History
202 lines (144 loc) · 6.34 KB

rest-fundamentals.md

File metadata and controls

202 lines (144 loc) · 6.34 KB

RESTful Web APIs

Introduction to RESTful Web APIs

What is REST?

  • Representational state transfer:
    • Originally for accessing and manipulating textual representations of Web resources using a set of stateless operations
    • Today: More generic, encompassing every entity that can be identified, named, addressed or handled, in any way whatsoever, on the Web
  • Architectural pattern, not a standard
  • Today, HTTP-based RESTful APIs dominate

Important Tools (Examples)

Sample Requests

<!--#include file="rest-fundamentals/0010-rest-clients/pokeapi.http" -->
<!--#include file="rest-fundamentals/0010-rest-clients/northwind.http" -->

Exercise: Try this sample with different REST clients

Sample Requests (cont.)

<!--#include file="rest-fundamentals/0010-rest-clients/post.http" -->

Exercise: Try this sample with different REST clients

Important REST principles

  • Stateless
    • No client context stored on the server
    • Each request is complete
  • Cacheable
    • Responses explicitly indicate their cacheability
  • Layered System
    • Client cannot tell if connected directly to the server (e.g. reverse proxies)
  • URIs
    • Resources are identified using Uniform Resource Identifiers (URIs)
  • Resource representation
  • XML, JSON, Atom - today mostly JSON

RESTful Web APIs in the Browser

<!--#include file="rest-fundamentals/0020-rest-client/app-promise.js" -->

RESTful Web APIs in the Browser (cont.)

With async/await:

<!--#include file="rest-fundamentals/0020-rest-client/app.js" -->

RESTful Web APIs in the Browser (cont.)

With jQuery:

<!--#include file="rest-fundamentals/0020-rest-client/app-jquery.js" -->

Building RESTful Web APIs with Node.js

  • In practice, frameworks are used for that
  • Example: Express.js
    • Larger framework, not just for RESTful Web APIs
    • Very commonly used
    • Lots of plugins
  • Example: restify
    • Smaller framework specialized on RESTful Web APIs
    • Easy to use
    • Also quite common
    • We will use this framework in this course

RESTful Web API with restify

<!--#include file="rest-fundamentals/0030-restify-basics/app.ts" -->
<!--#include file="rest-fundamentals/0030-restify-basics/request.http" -->

RESTful Web API with restify

  • server object
    • Register routes and handlers for incoming requests
    • Created using the createServer() method
    • Documentation
  • request object
    • Represents the HTTP request
    • Use it to get headers, parameters, body, etc.
    • Documentation
  • response object
    • Represents the HTTP response
    • Use it to build response (e.g. status, headers, body, etc.)
    • Documentation

restify Examples

<!--#include file="rest-fundamentals/0040-restify-verbs/app.ts" -->

restify Examples (cont.)

<!--#include file="rest-fundamentals/0040-restify-verbs/data.ts" -->
<!--#include file="rest-fundamentals/0040-restify-verbs/get-all.ts" -->

restify Examples (cont.)

<!--#include file="rest-fundamentals/0040-restify-verbs/get-single.ts" -->

restify Examples (cont.)

<!--#include file="rest-fundamentals/0040-restify-verbs/post.ts" -->

restify Examples (cont.)

<!--#include file="rest-fundamentals/0040-restify-verbs/delete-single.ts" -->

Further Readings and Exercises