π REST API Essentials for Backend-Frontend Communication
Tags: Resources, Endpoints, HTTP Methods, Request, Response, Status Codes, Stateless, Path & Query Parameters
π― Purpose
REST (Representational State Transfer) is an architectural style for designing web APIs that use standard HTTP (Hypertext Transfer Protocol) methods to enable communication between clients and servers.
π± Origin
REST was introduced by Roy Fielding in his 2000 doctoral dissertation. The name reflects transferring a βrepresentationβ of resources (data) via stateless client-server communication.
π§ Essentials
Doc: developer.mozilla.org/en-US/docs/Web/HTTP

π Resources & Endpoints
- Resources are represented by URLs - Uniform Resource Locator (nouns, not verbs).
- Example:
GET https://jsonplaceholder.typicode.com/posts
β‘ Core HTTP Methods
- GET β Read
- POST β Create
- PUT β Update
- DELETE β Remove
π¦ Request & Response Format
- Mostly JSON (sometimes XML). Configured on server & sent as header param.
- Example Response:
{ "id": 12, "name": "Bob", "email": "bob@example.com" }
π Status Codes
200 OKβ Success201 Createdβ New resource created400 Bad Requestβ Invalid input401 Unauthorizedβ Not correctly authenticated403 Forbiddenβ No access permission404 Not Foundβ Resource missing500 Internal Server Errorβ Server issue
βοΈ Statelessness
- Each request contains all necessary info (server does not remember previous requests).
- Auth Token Example: Included in headers every time. β
Authorization: Bearer <token>
π Path and Query Parameters
- Path Params: Identify a specific resource, part of the URL.
β Example: Fetch user with ID 123.
βGET /users/123 - Query Params: Add filters, options, or pagination.
β Example: Fetch page 2 of users with role admin.
βGET /users?role=admin\&page=2
π Testing APIs
1οΈβ£ With Extension REST Client for VS Code:
- Create file
request.httpwith following content:
### πΉ GET (read a resource)
GET https://jsonplaceholder.typicode.com/comments
#GET https://jsonplaceholder.typicode.com/comments?postId=1
### πΉ POST (create a resource)
POST https://jsonplaceholder.typicode.com/posts
Content-Type: application/json
{
"title": "foo",
"body": "bar",
"userId": 1
}
### πΉ PUT (update a resource)
PUT https://jsonplaceholder.typicode.com/posts/1
Content-Type: application/json
{
"id": 1,
"title": "updated title",
"body": "new content",
"userId": 1
}
### πΉ DELETE (remove a resource)
DELETE https://jsonplaceholder.typicode.com/posts/1
- Click βSend Requestβ above each call inside VS Code.
2οΈβ£ With cURL (client URL):
- cURL: command-line tool for transferring data with URL syntax from or to servers.
# πΉ Install cURL if not yet installed:
sudo apt update; sudo apt install curl
# πΉ GET (read a resource)
curl -X GET https://jsonplaceholder.typicode.com/posts
# πΉ POST (create a resource)
curl -X POST https://jsonplaceholder.typicode.com/posts \
-H "Content-Type: application/json" \
-d '{"title":"foo","body":"bar","userId":1}'
# πΉ PUT (update a resource)
curl -X PUT https://jsonplaceholder.typicode.com/posts/1 \
-H "Content-Type: application/json" \
-d '{"id":1,"title":"updated title","body":"new content","userId":1}'
# πΉ DELETE (remove a resource)
curl -X DELETE https://jsonplaceholder.typicode.com/posts/1
π Security Basics
- Use HTTPS to ensure data is encrypted, secure, and protected from eavesdropping or tampering.
- Authentication with API keys, JWT, or OAuth2.
β‘ Common Pitfalls
- β Missing
Content-Type: application/jsonheader when sending JSON. - β Using wrong HTTP method (
GETinstead ofPOST, etc.). - β Forgetting authentication tokens or API keys.
- β Hardcoding values instead of parameterizing requests.
- β Mixing them up param types β Use Path Params for identity, Query Params for filtering/options.
- β Forgetting to URL-encode values (
?q=hello worldβ?q=hello%20world). - β Adding sensitive data (e.g., passwords, tokens) in query params β use headers/body instead.
- β Using too many query params instead of designing clear endpoints.
All cheat sheets are taken from our Udemy Online Course. Interested? Check out:
All-In-One Full Stack DevOps - From Idea to Cloud