# RESTful Principles

> An architectural style for designing networked applications that emphasizes resources, standard HTTP methods, and stateless interactions.

Subject: Internet | Lesson 15 of 35 | 4 min read
Interactive lesson: https://codewithanimation.in/internet/restful-principles
Previous: [What is an API?](https://codewithanimation.in/internet/what-is-an-api.md) | Next: [Idempotency](https://codewithanimation.in/internet/idempotency.md)

## What it is

REST (Representational State Transfer) is an architectural style for designing networked applications.

Unlike arbitrary HTTP endpoints (like RPC over HTTP) which often use varied, custom URL structures and rely heavily on GET/POST for all actions, REST emphasizes resource-based routing, standard HTTP methods (`GET`, `POST`, `PUT`, `DELETE`, `PATCH`), standard representations (like JSON), and stateless interactions.

## Why it matters

It brings predictability and standardization to API design. Front-end developers consume APIs constantly; a REST API provides a predictable structure.

If a developer knows the resource is `/users`, they immediately know how to fetch (`GET`), create (`POST`), update (`PUT`/`PATCH`), and delete (`DELETE`) users without having to read extensive custom documentation. It beautifully decouples the client from the server.

## How it works

**Resources & URIs:** Everything is a resource identified by a unique URI (e.g., `/articles`, `/users/123`).

**HTTP Methods:** Standard verbs represent actions to be performed on the resources (`GET` for read, `POST` for create, `PUT`/`PATCH` for update, `DELETE` for remove).

**Representations:** Resources are transferred in a standard format, most commonly JSON.

**Statelessness:** Every request contains all the information needed to execute it. The server does not store client context (like sessions) between requests.

## Common mistake: Common mistake

The #1 common mistake is using **verbs in URLs** (e.g., `/getUsers` or `/deletePost` instead of using standard methods like `GET /users` and `DELETE /posts/{id}`). REST relies on HTTP verbs, not URL string verbs.

## Tip: Pro tip

Think in **'Nouns', not 'Verbs'**. Your URLs should represent things (nouns like `/products`), and your HTTP methods should provide the action (verbs like `GET` or `POST`).

## Try it: Try it

Use the **Drag-and-Drop URL Builder**. Drag a Method block and an Endpoint block into the slots to construct a valid REST request. Don't put verbs in the URL!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/restful-principles

## Check yourself

### Question 1

What is the primary focus of RESTful URIs compared to arbitrary HTTP endpoints?

- A. Identifying actions using verbs (e.g., `/getUser?id=123`).
- B. Identifying the current server session state.
- C. Identifying resources using nouns (e.g., `/users/123`).
- D. Mapping directly to database table names.

Correct answer: C

REST focuses on resources (nouns) and utilizes standard HTTP methods (verbs) to perform actions on those resources, keeping URIs clean and predictable.

- Why not A: Using verbs in URIs is characteristic of RPC-style endpoints, not RESTful architecture.
- Why not B: REST is inherently stateless; URIs identify abstract resources, not transient state.
- Why not D: While they might correspond to tables, URIs represent abstract resources and don't have to mirror database schemas.

### Question 2

Which HTTP method is conventionally used to create a new resource in a RESTful API?

- A. GET
- B. CREATE
- C. PUT
- D. POST

Correct answer: D

POST is the standard HTTP method used to submit data to an endpoint in order to create a newly subordinate resource.

- Why not A: GET is meant for retrieving data. It should be idempotent and never used to modify server state.
- Why not B: CREATE is a database CRUD operation, not a standard HTTP method.
- Why not C: PUT is typically used for replacing or entirely updating an existing resource, whereas POST is standard for creating new ones.

### Question 3

What does 'statelessness' mean in the context of REST architecture?

- A. The server is not allowed to have a database.
- B. The client cannot store any data locally.
- C. Each request from the client must contain all the information needed for the server to understand and process it.
- D. The connection between client and server remains open indefinitely.

Correct answer: C

Statelessness ensures that each request is completely independent. This makes the system more scalable, as the server doesn't need to retain previous interaction context.

- Why not A: Servers definitely use databases to persist resources. Statelessness refers to session state, not persistent data storage.
- Why not B: Clients can (and often do) store data like authentication tokens locally, which they must then send with each request.
- Why not D: This describes persistent connections like WebSockets. REST interactions are discrete request-response cycles.

## Remember this

- REST relies on Resources (Nouns) identified by URIs.
- Use standard HTTP Methods (Verbs) to interact with resources.
- REST is stateless; every request must contain all necessary context.
