Home FastAPI (3) REST Architecture(English)
Post
Cancel

FastAPI (3) REST Architecture(English)

REST Architecture

REST stands for Representational State Transfer, not rest architectureㄡ

Its purpose is to facilitate the exchange of information between different software/programs over the network.

Benefits

There are many benefits, but here are the most representative ones

Simplicity:REST architecture style adopts simple principles and constraints, making it easy to understand and implement. It uses standard methods in the HTTP protocol (GET, POST, PUT, DELETE, etc.), allowing developers to quickly get started. The following image from Wikipedia illustrates this concept clearly.

Desktop View

Independence:REST allows clients and servers to be technically independent. Their communication is only through the representation of resources (usually JSON or XML). Clients only need to call the API without worrying about the language used behind the API.

Security:RESTful APIs can be protected through standard HTTP security measures like HTTPS. This ensures the security and privacy of data transmission.

Easy to Test and Debug :Since RESTful APIs are stateless, they are easier to test and debug because each request is independent.

GET、POST、PUT、DELETE in Web Technology

GET & POST are supported by HTML forms, and AJAX requests also support them.

GET -> Corresponds to Read in CRUD

1
2
3
<form action="https://example.com/resource" method="get">
  <input type="submit" value="GET Resource">
</form>

POST -> Corresponds to Create in CRUD

1
2
3
<form action="https://example.com/resource" method="post">
  <input type="submit" value="POST Resource">
</form>

PUT -> Corresponds to Update in CRUD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<button onclick="putData()">PUT Resource</button>

<script>
  function putData() {
    fetch('https://example.com/resource', {
      method: 'PUT',
      body: JSON.stringify({ key: 'new-value' }),
      headers: {
        'Content-Type': 'application/json'
      }
    })
      .then(response => response.json())
      .then(data => console.log(data));
  }
</script>

DELETE -> Corresponds to Delete in CRUD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<button onclick="deleteData()">DELETE Resource</button>

<script>
  function deleteData() {
    fetch('https://example.com/resource', {
      method: 'DELETE'
    })
      .then(response => {
        if (response.status === 204) {
          console.log('Resource deleted successfully');
        }
      });
  }
</script>

Stateless

It’s important to explain what stateless means: each client request and server interaction is independent, and the server does not retain any information from one request to the next. This means each request must contain all necessary information. This is why RESTful APIs are easy to test.

Wait! “Each request must contain all necessary information,” which includes sensitive information such as account credentials. How can security be enhanced in this regard? Typically, authentication, OAuth 2.0, HTTPS, SSL/TLS, and CSRF are used to increase security.

Further details will be added as necessary.

☝ツ☝

This post is licensed under CC BY 4.0 by the author.

👈 ツ 👍