Skip to main content

Web API Interview Questions

🎯 Focus Areas

REST principles, JWT auth flow, HTTP verbs, status codes, Swagger, security. Critical for any backend developer role.


Q1: What is REST?

Representational State Transfer β€” architectural style for APIs.

6 REST constraints:
1. Client-Server β†’ UI and data separated
2. Stateless β†’ each request has all info needed, no server session
3. Cacheable β†’ responses can be cached
4. Uniform Interface β†’ consistent URL + HTTP verbs
5. Layered System β†’ client doesn't know if talking to server or proxy
6. Code on Demand β†’ optional (JavaScript download)

REST is NOT a protocol β€” it is a set of guidelines.

Q2: What are HTTP verbs and when to use each?

GET β†’ retrieve data (no body, safe, idempotent)
POST β†’ create new resource (has body, NOT idempotent)
PUT β†’ replace entire resource (has body, idempotent)
PATCH β†’ update partial resource (has body)
DELETE β†’ remove resource (no body, idempotent)

Idempotent = same result if called multiple times.
GET /students/1 β†’ always returns same student
PUT /students/1 β†’ always results in same state
DELETE /students/1 β†’ first call deletes, subsequent calls: 404

Q3: What HTTP status codes must you know?

2xx Success:
200 OK β†’ GET, PUT success
201 Created β†’ POST success (include Location header)
204 No Content β†’ DELETE success

4xx Client Error:
400 Bad Request β†’ validation failed, malformed JSON
401 Unauthorized β†’ no token or invalid token
403 Forbidden β†’ valid token but no permission
404 Not Found β†’ resource doesn't exist
409 Conflict β†’ duplicate, business rule violation
422 Unprocessable→ validation errors (often used instead of 400)

5xx Server Error:
500 Internal β†’ unhandled exception in your code
503 Unavailable β†’ service down, overloaded

Q4: What is JWT and how does it work?

JSON Web Token β€” compact, self-contained token for authentication.

3 parts (base64 encoded, separated by .):
Header.Payload.Signature

Header: { "alg": "HS256", "typ": "JWT" }
Payload: { "sub": "1", "name": "Ravi", "role": "Student", "exp": 1234567890 }
Signature: HMACSHA256(header + "." + payload, secret)

Flow:
1. POST /auth/login { email, password }
2. Server validates β†’ creates JWT β†’ returns token
3. Client stores token (localStorage or httpOnly cookie)
4. Each request: Authorization: Bearer {token}
5. Server validates signature β†’ reads claims β†’ authorizes

Q5: What is the difference between Authentication and Authorization?

Authentication β†’ WHO are you? (verify identity β€” JWT, cookies, API key)
Authorization β†’ WHAT can you do? (check permissions β€” roles, policies)

Authentication comes first β€” you must know who the user is
before checking what they can do.

[Authorize] // any authenticated user
[Authorize(Roles = "Admin")] // admin role only
[Authorize(Policy = "CanGrade")] // custom policy

Q6: What is Swagger/OpenAPI?

OpenAPI β†’ specification standard for describing REST APIs (JSON/YAML)
Swagger β†’ tools built around OpenAPI (UI, editor, codegen)
Swashbuckle β†’ .NET library that generates OpenAPI from C# code

In ASP.NET Core:
dotnet add package Swashbuckle.AspNetCore
β†’ auto-generates interactive API docs at /swagger
β†’ "Try it out" button tests endpoints in browser
β†’ Should be disabled in production (security risk)

Q7: What is the difference between IActionResult and ActionResult<T>?

// IActionResult β€” no type info for Swagger
public IActionResult GetStudent(int id)
{
return Ok(student); // Swagger doesn't know return type
}

// ActionResult<T> β€” type info available (better Swagger docs)
public ActionResult<StudentDto> GetStudent(int id)
{
return Ok(student); // Swagger knows this returns StudentDto
}

Q8: What is a DTO?

Data Transfer Object β€” shape of data sent/received by API.
Separate from domain model β€” controls what is exposed.

Why:
- Hide internal fields (PasswordHash, internal IDs)
- Shape data differently for different endpoints
- Prevent over-posting (mass assignment) attacks
- Version API independently of database schema

StudentDto { Id, Name, ClassName, Percentage } // no PasswordHash, no navigation props

Q9: What is CORS?

Cross-Origin Resource Sharing β€” browser security policy.
Blocks browser JS from calling API on different domain.

// Allow React app on localhost:4200 to call API on localhost:5001
builder.Services.AddCors(o => o.AddPolicy("AllowFrontend", p =>
p.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()));

app.UseCors("AllowFrontend");

Q10: What is API versioning?

πŸ’» Try It β€” Console App
πŸ’‘ Paste into Program.cs and press F5βŒ₯ GitHub
// URL versioning (most common)
GET /api/v1/students
GET /api/v2/students

// Header versioning
GET /api/students
X-API-Version: 2

// Why version: add breaking changes without breaking existing clients.
πŸ’‘ Top Web API Interview Questions
  1. REST constraints β€” know all 6
  2. HTTP verbs β€” when to use PUT vs PATCH vs POST
  3. JWT flow β€” draw it end to end
  4. Status codes β€” 400 vs 401 vs 403 vs 404 vs 422
  5. Authentication vs Authorization β€” simple clear answer
πŸ€–Use AI to Learn Faster

Use ChatGPT, Claude, or Copilot to go deeper on Web API REST interview questions. Try these prompts:

  • "Ask me 10 Web API interview questions and evaluate my answers"
  • "What is the most common wrong answer about JWT in .NET interviews?"
  • "Explain the difference between 401 and 403 in a scenario-based question"
  • "Quiz me on HTTP status codes β€” describe a scenario and I give the status code"

πŸ’‘ Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" β€” fastest way to find edge cases and deepen understanding.

nexcoding.in