Restful API design Flashcards

1
Q

What

A

Identify the Goal: What problem does the API solve? What resources will it manage?
Set Functionalities: List actions the API will support, like creating, reading, updating, or deleting resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Identify Resources

A

Resources represent the main entities in your API and are typically nouns.
Users
Orders
Products

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Use Resource URIs

A

Design resource URLs that are hierarchical, predictable, and meaningful.
Use nouns to represent resources.
Use plural for resource names (e.g., /users instead of /user).
/users – Get all users or create a new user.
/users/{id} – Access a specific user by ID.
/users/{id}/orders – Access orders for a specific user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Map to HTTP Methods

A

GET: Retrieve resources.
POST: Create new resources.
PUT: Update an existing resource (replace the whole).
PATCH: Partially update a resource.
DELETE: Remove a resource.

GET /users – Fetch all users.
POST /users – Create a new user.
PUT /users/{id} – Update user details.
DELETE /users/{id} – Delete a user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Versioning Handling

A

API versions ensure backward compatibility when introducing changes
GET /v1/users

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Error Codes

A

200 OK: Successful GET or PUT request.
201 Created: Resource successfully created.
204 No Content: Successful DELETE request.
400 Bad Request: Invalid input.
401 Unauthorized: Authentication failure.
404 Not Found: Resource not found.
500 Internal Server Error: Server failure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Input Mechanisms For APIs

A

Query Params
Path Params
Request Body
HTTP Headers
Form Data
Cookies

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Query Param

A

GET /users?name=John&age=30
Passed in the URL after ? and separated by &.
Used for filtering, pagination, and optional parameters.

Used with GET, DELETE etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Path Parameters

A

Embedded in the URL path to specify a resource.
GET /users/{id}
Used with GET, PUT, DELETE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Request Body

A

Sends data in JSON, XML, or form format.

Used with POST, PUT, PATCH
POST /users
Content-Type: application/json

{
“name”: “John”,
“age”: 30
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

HTTP Headers

A

Passes metadata such as authentication tokens, content type, or custom headers.
GET /users
Authorization: Bearer <token>
Content-Type: application/json</token>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Form Data

A

Used for submitting forms (application/x-www-form-urlencoded or multipart/form-data).
Example (application/x-www-form-urlencoded
POST /submit-form
Content-Type: application/x-www-form-urlencoded

name=John&age=30
Example (multipart/form-data for file upload
POST /upload
Content-Type: multipart/form-data; boundary=—XYZ

—XYZ
Content-Disposition: form-data; name=”file”; filename=”image.jpg”
Content-Type: image/jpeg

[binary data]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Cookies

A

Used for authentication or session management.
GET /dashboard
Cookie: session_id=abcd1234

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Request Body vs Form Data Input Type

A

Used when submitting HTML forms. Data is encoded as key-value pairs. Sent as part of the request body but encoded in a way that mimics query parameters.

Of 2 Types
application/x-www-form-urlencoded
Default for web forms (text data only). Data is URL-encoded (like query parameters).

POST /submit-form
Content-Type: application/x-www-form-urlencoded
name=John&age=30

multipart/form-data
Used for file uploads (binary data).
Each field is separated by a boundary.
POST /upload
Content-Type: multipart/form-data; boundary=—XYZ

—XYZ
Content-Disposition: form-data; name=”file”; filename=”image.jpg”
Content-Type: image/jpeg

[binary image data]

Request Body (application/json, application/xml, etc.)
Used when sending structured data (e.g., JSON, XML).
Data is sent as a raw payload.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Request Body vs Form Data Input Type - Server Handling

A

FORM DATA

POST /submit-form
Content-Type: application/x-www-form-urlencoded
name=John&age=30
@PostMapping(“/submit”)
public String handleForm(@RequestParam String name, @RequestParam int age) {

File Handling
@PostMapping(“/file”)
public String handleFileUpload(@RequestParam(“file”) MultipartFile file) {
return “Received File: “ + file.getOriginalFilename();

Request Body
POST /users
Content-Type: application/json

{
“name”: “John”,
“age”: 30
}

@PostMapping
public String handleJson(@RequestBody User user) {
return “Received JSON Data: Name - “ + user.getName() + “, Age - “ + user.getAge();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Browser Handling of Form Submission

A

Depending on the form’s method (GET or POST), the data is formatted accordingly.

<form>
The action attribute defines the target URL (/submit).
In case of GET - Data is appended to the URL as query parameters
In case of POST - Data is sent in the request body.
The server response replaces the current page.
If the response is an HTML page, it is rendered.
If the response is JSON, the browser may display raw JSON
If the server sends a redirect (302 Found), the browser automatically navigates to the new URL.
</form>

17
Q

Browser handing of Form Submission - Ajax Interception

A

Instead of the default behavior, JavaScript can prevent the form from reloading the page and send data asynchronously.
This allows real-time form submission without reloading the page.
document.getElementById(“myForm”).addEventListener(“submit”, function(event) {
event.preventDefault(); // Prevents full page reload

let formData = new FormData(this);

fetch('/submit', {
    method: 'POST',
    body: formData
}).then(response => response.json())
  .then(data => console.log(data)); });
18
Q

Redirects Use Cases

A

Preventing Duplicate Submissions
After a user submits a form, a server may respond with a redirect to prevent accidental resubmission on refresh.
User submits a POST request (/submit-form).
Server processes the request and responds with
HTTP/1.1 302 Found
Location: /thank-you
Browser follows the redirect to /thank-you using GET.
f the user refreshes the page, only the GET request reloads (not the POST).

19
Q

Redirect Method Type change

A

A redirect can change the HTTP method depending on the redirect status code used. Some redirects preserve the method (e.g., 307, 308), while others convert POST to GET (e.g., 302, 303).

20
Q

Authorization Header

A

The Authorization header is used in HTTP requests to authenticate the client with the server. It contains credentials or tokens that allow the server to verify the client’s identity before granting access to resources.
General Format of the Authorization Header
Authorization: <type> <credentials></credentials></type>

<type> → Specifies the authentication scheme (e.g., Basic, Bearer, Digest)
<credentials> → Contains the encoded authentication information
</credentials></type>

21
Q

Authorization Schemes

A

Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
dXNlcm5hbWU6cGFzc3dvcmQ= is Base64(username:password).
Security Risk: Base64 is not encrypted, so it should only be used over HTTPS.
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9…
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9… is a JWT token.
Authorization: ApiKey my-secret-api-key
Digest Authentication -less common today

22
Q

Server handling of Authorization

A

401 - Unauthorized
403 - If credentials are required

23
Q

CORS

A

A CORS request happens when the client (frontend) is hosted on a different origin than the backend API.
Client on https://client.com makes a request to https://api.example.com.
GET /api/data HTTP/1.1
Origin: https://client.com
Host: api.example.com

Example from Frontend of makemytrip.com request is made to visa.com for payment

24
Q

Host Header vs Origin Header

A

Host Header: Defines which server the request is sent to (used for routing requests).

Origin Header: Defines where the request is coming from (used for security policies in browsers).

GET /pay HTTP/1.1
Host: payservice.com
Origin: https://shop.com

Host: payservice.com → The server is payservice.com, so it receives the request.

Origin: https://shop.com → The browser checks CORS policy because the request comes from a different origin.
Without CORS, the browser blocks the response before the frontend can read it.

25
Session Cookie
A session cookie is a small piece of data stored in a user's browser that helps maintain session state between requests. It is commonly used in web applications for authentication and tracking user interactions. Browser automatically sent back to the server with each subsequent request. The server sends the session ID to the client via a Set-Cookie HTTP header. Set-Cookie: SESSIONID=abc123xyz; Path=/; HttpOnly; Secure; SameSite=Strict
26
SessionId
The tomcat server creates a random, unique identifier (session ID) for the new session. This ID is usually a long, unpredictable string to prevent attacks like session fixation or session hijacking. The server sends the session ID to the client via a Set-Cookie HTTP header. Set-Cookie: SESSIONID=abc123xyz; Path=/; HttpOnly; Secure; SameSite=Strict
27
Tomcat generated sessionid
When a new session is created, Tomcat generates a random, unique session ID. This session ID is stored in the JSESSIONID cookie and sent to the client. Spring Boot relies on the servlet container (Tomcat by default) for session management. Set-Cookie: JSESSIONID=ABC123XYZ; Path=/; HttpOnly; Secure; SameSite=Strict
28
Spring Boot managed session id
To persist sessions outside Tomcat in a Spring Boot application, you can use Spring Session with a session store like Redis, JDBC (database), or Hazelcast. This ensures that session data survives Tomcat restarts and supports distributed applications. Add Spring Session and Redis Dependencies org.springframework.session spring-session-data-redis org.springframework.boot spring-boot-starter-data-redis
29
Can React have a backend component
Yes, a React app can have a backend component, but the backend is a separate service, not part of React itself. React itself does NOT have a backend but can communicate with one. How it Works: User loads the React app (HTML, JS, CSS from a web server or CDN). React (frontend) runs in the browser and makes API calls. The backend component (Node.js, Spring Boot, etc.) handles API requests. The backend queries a database (MySQL, MongoDB, etc.) and returns data. React updates the UI with the fetched data.
30
Server Side Rendering
Step 1: User Requests a Page The browser sends a request for a React page (e.g., GET /products). This request goes to a Node.js server (if using Next.js, for example). Step 2: Server Generates the Page The server fetches data from APIs or databases. React renders the page on the server using ReactDOMServer.renderToString(). The fully-rendered HTML is sent back to the browser.
31
SSR vs CSR HTTP Payload
SSR GET /products HTTP/1.1 Host: example.com User-Agent: Mozilla/5.0 Body - Full HTML File CSR Only skeleton HTML is sent
Since the HTML is empty, React must fetch data separately: GET /api/products HTTP/1.1
32
React App Working
No, the React app itself does not store or fetch data directly. Instead, React runs in the browser and makes API calls to a backend server to fetch data. How Data is Fetched in a React App (CSR Flow) The browser loads the React app (HTML + JavaScript). React runs in the browser and executes its logic. React makes an API request to a backend server (e.g., Node.js, Spring Boot, Express, etc.). The backend server fetches data from a database and sends it back as JSON. React updates the UI dynamically based on the API response.