> For the complete documentation index, see [llms.txt](https://go.raymondctw.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.raymondctw.dev/technology/web-development/restful-api-shi-shen-mo.md).

# RESTful API是什麼？

RESTful API是一種API設計的風格，REST為Representational State Transfer的簡稱，RESTful API則代表符合此風格的API設計。

如果你沒有學過RESTful API模式，你隨便用Flask或FastAPI建立的API可能長這樣子，所有的方法都是使用`GET` ，在後端才針對不同的目的做出不同的命令。

<table><thead><tr><th width="150.33333333333331">目的</th><th width="391">URL</th><th>Request Method<select><option value="779a3436f69944419c711f3d8c94b7e2" label="GET" color="blue"></option></select></th></tr></thead><tbody><tr><td>取得資料</td><td>http://127.0.0.1:8000/get-data/1</td><td><span data-option="779a3436f69944419c711f3d8c94b7e2">GET</span></td></tr><tr><td>新增資料</td><td>http://127.0.0.1:8000/insert-data/2</td><td><span data-option="779a3436f69944419c711f3d8c94b7e2">GET</span></td></tr><tr><td>更新資料</td><td>http://127.0.0.1:8000/update-data/3?value=5</td><td><span data-option="779a3436f69944419c711f3d8c94b7e2">GET</span></td></tr><tr><td>刪除資料</td><td>http://127.0.0.1:8000/delete-data/4</td><td><span data-option="779a3436f69944419c711f3d8c94b7e2">GET</span></td></tr></tbody></table>

如果你學會了RESTful API後，就會變成以下這樣，主要就是將執行的動作放在Request Method裡面。

<table><thead><tr><th width="153.33333333333331">目的</th><th width="392">URL</th><th>Request Method<select><option value="4f0cdf72c40e41a59139f9719c1c3630" label="GET" color="blue"></option><option value="2f78ec4a39e14a60bc977160059ddac3" label="POST" color="blue"></option><option value="9010114dec384c8abbd4eaff010fb9be" label="PUT" color="blue"></option><option value="075fea25e2ec481695078f3bc34d8a30" label="DELETE" color="blue"></option></select></th></tr></thead><tbody><tr><td>取得資料</td><td>http://127.0.0.1:8000/data/1</td><td><span data-option="4f0cdf72c40e41a59139f9719c1c3630">GET</span></td></tr><tr><td>新增資料</td><td>http://127.0.0.1:8000/data</td><td><span data-option="2f78ec4a39e14a60bc977160059ddac3">POST</span></td></tr><tr><td>更新資料</td><td>http://127.0.0.1:8000/data/2</td><td><span data-option="9010114dec384c8abbd4eaff010fb9be">PUT</span></td></tr><tr><td>刪除資料</td><td>http://127.0.0.1:8000/data/3</td><td><span data-option="075fea25e2ec481695078f3bc34d8a30">DELETE</span></td></tr></tbody></table>

{% hint style="info" %}
方法GET、POST、PUT與DELETE分別對應資料庫的CRUD。
{% endhint %}

## RESTful API的URI命名規則

* URI使用小寫
* URI使用複數名詞

```
http://127.0.0.1:8000/products
[GET] /products 回傳所有產品
[GET] /products/1 回傳單筆產品
[POST] /products 新增產品
[PUT] /products/4 修改單筆產品
[DELETE] /products/5 刪除單筆產品
```

## RESTful API安全性

在使用`PUT` 與`DELETE` 時代表修改資料庫的內容，此時必須加上權限驗證機制，例如使用token回傳，透過後端進行驗證。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://go.raymondctw.dev/technology/web-development/restful-api-shi-shen-mo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
