HTTP METHOD Summary

in Learn with 0 comment

HTTP METHOD Summary

A simple summary about the HTTP methods and not really systematic

Previous

Idempotent Method

The first question is how to understand the "What is idempotent?"
There is a very simple answer found from STACKOVERFLOW:
No matter how many times you call the operation, the result will be the same.
It's like a basic math function y=x+4 no matter how many times you call it f(4), it will give you the same answer 7.

Safe Method

In particular, the convention has been established that the GET and HEAD methods SHOULD NOT have the significance of taking an action other than retrieval. These methods ought to be considered "safe". This allows user agents to represent other methods, such as POST, PUT and DELETE, in a special way, so that the user is made aware of the fact that a possibly unsafe action is being requested.

User's action will not make any change to the recourses in the server that means the action/method is safe

PS. in the real product server GET method sometimes associate with the backend Database and do some queries which may cause side-effect, the important distinction here is that the user did not request the side-effects.

HTTP Method History

HTTP 0.9 had a single method: GET.
HTTP 1.0 had the methods: GET, HEAD, and POST.
HTTP 1.1, the current version, has the methods: GET, HEAD, POST, OPTIONS, PUT, DELETE, TRACE, and CONNECT.

Methods

CONNECT

For proxy usage

DELETE

DELETE /file.html HTTP/1.1

response code:202/204/200

HEAD

get meta-info/header of resources, for example use HEAD to get the file size before downloading it.

HEAD has no response content(should be ignored even it exists)

OPTIONS

get the options that the server accept/support

PUT

similar to upload and replace the resources in the server

this method is Idempotent

GET

use GET method to get specific info from server, if the info associate with the generation of info itself then the result should be return in the response content

POST

The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header.

A POST request is typically sent via an HTML form and results in a change on the server.

FEATURE COMPARE

CONNECTDELETEHEADOPTIONSPUTGETPOST
Request has bodyNoMayNONOYESNOYES
Successful response has bodyYesMayNOYESNOYESYES
SafeNoNoYESYESNOYESNO
IdempotentNoYesYESYESYESYESNO
CacheableNoNoYESNONOYESOnly if freshness info is included
Allowed in HTML formsNoNoNONONOYESYES
Responses