Skip to content

Restful API

Interactive Diagram

새 탭 ↗

ref) https://restfulapi.net/

  1. Uniform interface
    : Resources within the system each have a unique, meaningful URI (uniform resource identifier) and must be able to perform functions such as retrieving and assigning information
  2. Client-server
    : As long as the URI does not change, the client and server must be able to be developed/evolved without dependency on each other
  3. Stateless
    : The server processes each request on its own without retaining any information from previous or last requests. No session, No history
  4. Cacheable
    : Where possible, resource information should be cached.
    ::: tip Well-managed caching partially or completely eliminates some client-server interactions, further improving scalability and performance. :::
  5. Layered system
    : You can split the API, data, authentication, etc., across different servers
  6. Code on demand(optional)
    : When needed, you can deliver executable artifacts (code, rendered UI, etc.)
FeaturesGET MethodsPOST/PUT/DELETE
CacheableYesNo
Browser HistoryYesNo
BookmarkYesNo
LengthMax: 2048 charFree
Back button/ReloadHarmlessData re-submitted
Encodingapplication/x-www-form-urlencodedany
Data TypeOnly ASCIINo restrictions

Mainly used to request data.

http(s)://domain/[path]/[servlet]?key1=value&key2=value

Terminal window
curl --location --request GET 'https://api.bithumb.com/public/candlestick/BTC_KRW/1m'

Used to create or modify a resource by sending data,
where the data is placed in the body of the HTTP request.

  • POST vs PUT

    When the result is always identical (idempotent), PUT is generally used

POST [servlet_path] [HTTP/1.1 or HTTP/2]
Host: [Domain]
key11=value1&key2=value2
PUT [servlet_path] [HTTP/1.1 or HTTP/2]
Host: [Domain]
key11=value1&key2=value2
Terminal window
curl --location --request POST 'http(s)://[domain]/[servlet_path]' \
--header 'Content-Type: application/json' \
--data-raw '{
"key1": "value",
"key2": "value"
}'

Also called the maturity model; this section explains the definition and characteristics of each level.

Level 0 does not use web mechanisms and uses HTTP simply as a transport system for remote communication. It is an HTTP API designed in RPC (Remote Procedure Call) form without distinguishing resources. It refers to an RPC-style system that simply exchanges POX (Plain Old XML). It uses a single endpoint, and that one endpoint performs multiple operations based on the different parameters passed to it. To pass parameters in the body, the HTTP method becomes POST.

A REST ā€˜resource’ is the core piece of data the application operates on. This often corresponds to the application’s model (especially when following the MVC (Model, View, Controller) pattern). Level 1 API design is about using different URLs to interact with the various resources of the application.

Since we always have to perform CRUD operations on resources, why not find a way to share these operations across resources? Why create a new resource for each operation or task you want to perform? We do this using HTTP verbs. To get a list of Pages, you send a GET request to /page, but to create a new Page you use the same resource /page with POST instead of GET.

HTTP headers are one of the fundamental design rules of a REST API. They are needed to convey more data about the resource itself, primarily metadata, security, hashes, etc.

Another important part of REST API design is the use of query parameters. They are widely used in many cases, but more often they are used to perform some kind of search, filtering, and querying.

As a RESTful API, using proper HTTP status codes is very important, especially when developing and mocking RESTful APIs.

  • 200 : Everything is working.
  • 201 : Created. A new resource has been created.
  • 204 : No Content. The resource was successfully deleted and there is no response body.
  • 304 : NOT MODIFIED. The returned data is cached data (the data has not changed).
  • 400 : BAD REQUEST. The request is malformed or cannot be processed. The exact error should be described in the error payload.
    e.g. ā€œJSON is not validā€.
  • 401 : UNAUTHORIZED. The request requires user authentication.
  • 403 : FORBIDDEN. The server understood the request but refuses it or access is not allowed.
  • 404 : Not Found. There is no resource behind the URI.
  • 500 : Internal Server Error. API developers should avoid this error. If an error occurs in the global catch block, the stack trace should be logged and not returned in the response.

This has two parts: content negotiation and HATEOAS. Content negotiation focuses on the various representations of a particular resource, and HATEOAS is about the discoverability of actions on a resource. This level is hard and difficult to apply. As an alternative, the GraphQL concept has emerged and is gradually spreading.

In this approach, each resource responds with the information of the provided resource along with an API spec that lets you request detailed or related information. In other words, the API requester can leverage detailed APIs by knowing only the core REST API, without detailed knowledge of the API spec.

HTTP/1.1 200 OK
Content-Type: application/json
{
"result" {
"name": "Ohjongsung",
"id": "1",
"nextActions": {
"/api/users/{userId}/roles",
}
}
}