πŸš€ 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

REST Essentials


πŸ”— 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 β†’ Success
  • 201 Created β†’ New resource created
  • 400 Bad Request β†’ Invalid input
  • 401 Unauthorized β†’ Not correctly authenticated
  • 403 Forbidden β†’ No access permission
  • 404 Not Found β†’ Resource missing
  • 500 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.http with 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/json header when sending JSON.
  • ❌ Using wrong HTTP method (GET instead of POST, 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