iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

HTTP Methods

HTTP methods (also called verbs) tell the server what kind of action the client wants. Pick the right one and your API stays predictable.

The common methods

MethodPurposeIdempotent?Safe?
GETRead a resource.YesYes
HEADSame as GET, but only headers.YesYes
OPTIONSAsk what methods/headers are allowed (used by CORS).YesYes
POSTCreate a resource or submit data.NoNo
PUTReplace a resource entirely.YesNo
PATCHPartially update a resource.NoNo
DELETERemove a resource.YesNo

Idempotent vs safe

  • Safe: the method doesn't change anything on the server. Browsers can prefetch, cache, and replay safely.
  • Idempotent: calling it twice has the same effect as calling it once. Allows retry on network failure.

REST in one line

GET    /posts        # list
GET    /posts/42     # read one
POST   /posts        # create
PUT    /posts/42     # replace
PATCH  /posts/42     # partial update
DELETE /posts/42     # delete
Tip: HTML forms only natively support GET and POST. Frameworks (Laravel, Rails) emulate PUT, PATCH, and DELETE by sending a hidden _method field.

Example

Example
<!DOCTYPE html>
<html>
<head>
    <title>HTTP Methods</title>
</head>
<body>

<h1>HTTP Methods</h1>
<p>This is a demo page for the "HTTP Methods" lesson.</p>

</body>
</html>
Try it Yourself »

Exercise

Which HTTP method is used to fetch (read) a resource without changing it?

/users

Test yourself

Q1. Read-only method is…
Q2. Create a resource with…
Q3. Idempotent update is…

Discussion

Loading…