TutorialsAPIMethods & Status Codes
API

Methods & Status Codes

HTTP methods (verbs)

Each method describes an action on a resource:

  • GET — retrieve data. Safe (no changes) and idempotent. Parameters go in the URL.
  • POST — create a new resource. Not idempotent (calling twice creates two).
  • PUT — replace a resource entirely. Idempotent.
  • PATCH — partially update specific fields.
  • DELETE — remove a resource. Idempotent.
💡

Idempotent means making the same request multiple times has the same effect as making it once. GET, PUT, and DELETE are idempotent; POST is not.

Sending data with a request

// POST with a JSON body
await fetch("/api/users", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "Priya", email: "p@example.com" }),
});
💡

You must set the Content-Type: application/json header and JSON.stringify() the body — fetch sends a string, not an object.

Status codes

The server's response includes a 3-digit status code, grouped by first digit:

RangeMeaningCommon codes
2xxSuccess200 OK, 201 Created, 204 No Content
3xxRedirection301 Moved, 304 Not Modified
4xxClient error400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 429 Too Many Requests
5xxServer error500 Internal Error, 503 Unavailable

Using status codes correctly

const res = await fetch("/api/users/999");

if (res.status === 404) {
  console.log("User not found");
} else if (res.status === 401) {
  console.log("Please log in");
} else if (res.ok) {
  const user = await res.json();
}

Returning the right status code lets clients handle responses programmatically instead of parsing error messages.

Watch & Learn

A recommended video to watch alongside this chapter.

More “Methods & Status Codes” videos on YouTube