---
# System prepended metadata

title: HTTP 請求方法requests method(POST、GET、PUT、PATCH、DELETE、OPTIONS、HEAD)
tags: [restful api, AJAX]

---

# HTTP 請求方法requests method(POST、GET、PUT、PATCH、DELETE、OPTIONS、HEAD)

###### tags: `restful api` `AJAX`


Method Idempotent解說請參考([restful api 串接 0](https://hackmd.io/nYwH9-OiSXWzVrlSZH8uWw))

| HTTP | Method Idempotent | Safe |
| :-- | :-- |:--:|
| POST| no |no|
| GET  | yes | yes |
| PUT| yes |no|
| PATCH| no |no|
| DELETE| yes |no|
| OPTIONS| yes |yes|
| HEAD| yes |yes|

w3schools [HTTP Request Methods](https://www.w3schools.com/tags/ref_httpmethods.asp) 對以上七種Methods解說

### POST

**POST is used to send data to a server to create/update a resource.**
The data sent to the server with POST is stored in the request body of the HTTP request:

```
POST /test/demo_form.php HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2
```

**POST is one of the most common HTTP methods.**

**Some other notes on POST requests:**

* POST requests are never cached
* POST requests do not remain in the browser history
* POST requests cannot be bookmarked
* POST requests have no restrictions on data length

**POST用於新建/更新data到server
傳送到server的data是存在http請求者端身上**

* POST requests 不會放在緩存資料裡
* POST requests 不保存在瀏覽器歷史紀錄
* POST requests 無法添加到書籤
* POST requests 對數據長度沒有限制

MDZ [HTTP requests](https://developer.mozilla.org/zh-TW/docs/Web/HTTP/Methods/POST) 進一步解說

A POST request is typically sent via an HTML form and results in a change on the server. In this case, the content type is selected by putting the adequate string in the enctype attribute of the <form> element or the formenctype attribute of the <input> or <button> elements:


POST request 通常通過HTML表單發送,並導致服務器資料被修改.通過將適當的字符串放在<form><input> or <button> 元素的編碼型別,來選擇內容類型.

### GET
**GET is used to request data from a specified resource.**

**GET is one of the most common HTTP methods.**

Note that the query string (name/value pairs) is sent in the URL of a GET request:

`/test/demo_form.php?name1=value1&name2=value2`

**Some other notes on GET requests:**

* GET requests can be cached
* GET requests remain in the browser history
* GET requests can be bookmarked
* GET requests should never be used when dealing with sensitive data
* GET requests have length restrictions
* GET requests are only used to request data (not modify)

GET requests 通常用於從指定資源請求數據

請注意，查詢字符串（名稱/值對）是在GET請求的URL中發送的：

* GET requests 可以被緩存
* GET requests 保存在瀏覽器記錄歷史中
* GET requests 可以添加到書籤
* GET requests 不該使用於處理敏感資料
* GET requests 有長度限制
* GET requests 僅用於請求數據(不可修改)

### PUT
**PUT is used to send data to a server to create/update a resource.**

The difference between POST and PUT is that PUT requests are idempotent. That is, calling the same PUT request multiple times will always produce the same result. In contrast, calling a POST request repeatedly have side effects of creating the same resource multiple times.

PUT用於將數據發送到伺服器以建立/更新資源
POST和PUT之間的區別在於PUT請求是冪等的。 也就是說，多次調用相同的PUT請求將始終產生相同的結果。 相反，重複調用POST請求具有多次創建相同資源的副作用。

POST和PUT都可以更新伺服器資源,更改指定URI的語義,但更改方式不同

以電腦資料更新方式來舉例
**POST**:在資料夾裡面新增一個新的檔案,來達到新增目的.所以calling a POST request repeatedly have side effects of creating the same resource multiple times.重複調用POST請求具有多次創建相同資源的副作用。

PUT:從A點複製一份,覆蓋掉B點原有資料,因為A還是=B,不管操作幾次複製貼上,A和B都是一樣,結果不變的,所以PUT是冪等的.

### PATCH
修改部分功能

### [GET vs. POST](https://www.w3schools.com/tags/ref_httpmethods.asp) 比較

| | GET | **POST** |
| :-- | :-- | :--  |
| BACK button/Reload| Harmless |Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted)|
| Bookmarked| Can be bookmarked |Cannot be bookmarked|
| Cached| Can be cached |Not cached|
| Encoding type| noapplication/x-www-form-urlencoded |application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data|
| History| Parameters remain in browser history |Parameters are not saved in browser history|
| Restrictions on data length| Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters)|No restrictions|
| Restrictions on data type| Only ASCII characters allowed |No restrictions. Binary data is also allowed|
| Security| GET is less secure compared to POST because data sent is part of the URL Never use GET when sending passwords or other sensitive information! |POST is a little safer than GET because the parameters are not stored in browser history or in web server logs|
| Visibility| 	Data is visible to everyone in the URL |Data is not displayed in the URL|

| | GET | **POST** |
| :-- | :-- | :--  |
| 上一步按鈕/重整| 無害 |數據將被重新提交（瀏覽器應警告用戶數據將被重新提交）|
| 加入書籤| 可以加入書籤 |不可加入書籤|
| 緩存| 可以緩存 |不可緩存|
| 編碼方式| noapplication/x-www-form-urlencoded |application/x-www-form-urlencoded or multipart/form-data. 對二進制數據使用分段編碼|
| 歷史紀錄| 參數保留在瀏覽器歷史記錄中 |參數不保留在瀏覽器歷史記錄中|
| 數據長度限制| 是的，在發送數據時，GET methods將數據添加到URL。 並且網址的長度受到限制（最大網址長度為2048個字符）|無限制|
| 數據類型限制| 只允許使用ASCII字符 |無限制。 二進制數據也被允許|
| 安全性| 與POST相比，GET的安全性較差，因為發送的數據是URL的一部分，在發送密碼或其他敏感信息時切勿使用GET！ |POST比GET安全一些，因為參數未存儲在瀏覽器歷史記錄或Web服務器日誌中|
| 能見性| 	網址中的所有人都可以看到數據 |數據未顯示在URL中|