Theo định nghĩa của HTTP 1.0 https://tools.ietf.org/html/rfc2616#section-9.6 chỉ rõ:
The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI, it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.
Theo specification điểm khác biệt cơ bản giữa POST và PUT là ở POST, chúng ta request tới một URI mà URI đó được định nghĩa sẽ là accept resource chúng ta gửi mà server chưa có thí dụ:
- POST: http://localhost:8080/resources
Còn PUT thì request tới 1 URI mà URI định nghĩa trực tiếp 1 resource có sẵn trong server thí dụ:
- PUT: http://localhost:8080/shopping/carts/132
Cách sử dụng:
POST được dùng để tạo 1 resource mới, còn PUT thường được dùng gọi để update 1 resource.
Thực chất chúng chỉ khác nhau về mặt concept. Còn về mặt implementation thì POST làm được những gì thì PUT cũng làm được cái đó và ngược lại. Hầu hết các trường hợp chúng ta dùng POST nhưng trong trường hợp sau đây thì chúng ta sử dụng PUT sẽ tốt hơn:
- Khi làm việc trên 1 resource có sẵn và resource đó phải là idempotent ( Có nghĩa là khi mình gọi đến resource dùng PUT n lần thì behavior vẫn là như nhau, dữ liệu thay đổi của lần thứ n vẫn giống như lần gọi đầu tiên)