Showing 30 question(s)
Answer:
REST (Representational State Transfer) API is an architectural style for designing web services that use HTTP methods to perform operations on resources. REST APIs are stateless, scalable, and commonly exchange data in JSON format.
Code Example:
GET https://api.example.com/usersAnswer:
The key principles of REST are Client-Server architecture, Stateless communication, Cacheable responses, Uniform Interface, Layered System, and Optional Code on Demand.
Code Example:
Client
│
HTTP Request
│
Server
│
HTTP ResponseAnswer:
A resource is any object, data, or service that can be accessed by a client. Each resource is identified by a unique URI.
Code Example:
GET /users
GET /users/101
GET /products/10Answer:
A URI (Uniform Resource Identifier) uniquely identifies a resource in a REST API.
Code Example:
https://api.example.com/users/100Answer:
REST is lightweight, uses HTTP and JSON/XML, and is easier to develop. SOAP is a protocol that primarily uses XML, has strict standards, and includes built-in security and transaction support.
Code Example:
REST → JSON + HTTP
SOAP → XML + HTTPAnswer:
The common HTTP methods are GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS. Each method performs a specific operation on resources.
Code Example:
GET
POST
PUT
PATCH
DELETEAnswer:
GET retrieves one or more resources from the server without modifying any data.
Code Example:
GET /api/employees
Response:
200 OKAnswer:
POST creates a new resource on the server using the data sent in the request body.
Code Example:
POST /api/employees
{
"name":"John",
"salary":50000
}Answer:
PUT completely replaces an existing resource with the data provided in the request body.
Code Example:
PUT /api/employees/1
{
"id":1,
"name":"John",
"salary":60000
}Answer:
PATCH partially updates an existing resource by modifying only the specified fields instead of replacing the entire resource.
Code Example:
PATCH /api/employees/1
{
"salary":70000
}Answer:
The DELETE method removes an existing resource from the server. If the operation is successful, the server typically returns HTTP status code 200 OK, 202 Accepted, or 204 No Content.
Code Example:
DELETE /api/employees/1Answer:
PUT replaces the entire resource with the request payload, while PATCH updates only the specified fields of an existing resource.
Code Example:
PUT /api/users/1
{
"id":1,
"name":"John",
"email":"john@example.com"
}
PATCH /api/users/1
{
"email":"john123@example.com"
}Answer:
HTTP status codes indicate the result of an HTTP request. They help clients understand whether a request was successful or if an error occurred.
Code Example:
200 OK
201 Created
400 Bad Request
401 Unauthorized
404 Not Found
500 Internal Server ErrorAnswer:
HTTP 200 OK indicates that the request was successful and the server returned the requested resource.
Code Example:
GET /api/products
HTTP/1.1 200 OKAnswer:
HTTP 201 Created indicates that a new resource has been successfully created on the server after a POST request.
Code Example:
POST /api/products
HTTP/1.1 201 CreatedAnswer:
HTTP 400 Bad Request indicates that the request sent by the client is invalid due to malformed syntax or incorrect data.
Code Example:
HTTP/1.1 400 Bad RequestAnswer:
HTTP 401 Unauthorized indicates that authentication is required or the provided credentials are invalid.
Code Example:
HTTP/1.1 401 UnauthorizedAnswer:
HTTP 403 Forbidden indicates that the client is authenticated but does not have permission to access the requested resource.
Code Example:
HTTP/1.1 403 ForbiddenAnswer:
HTTP 404 Not Found indicates that the requested resource could not be found on the server.
Code Example:
GET /api/products/100
HTTP/1.1 404 Not FoundAnswer:
HTTP 500 Internal Server Error indicates that an unexpected error occurred on the server while processing the request.
Code Example:
HTTP/1.1 500 Internal Server ErrorAnswer:
Authentication is the process of verifying the identity of a client before allowing access to protected REST API resources.
Code Example:
GET /api/users
Authorization: Bearer <access_token>Answer:
Authorization determines what authenticated users are allowed to access or perform after their identity has been verified.
Code Example:
User Role: Admin
✔ Read
✔ Create
✔ Update
✔ DeleteAnswer:
JWT is a compact, URL-safe token used for securely transmitting user information between a client and server. It is commonly used for authentication in REST APIs.
Code Example:
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...Answer:
A JWT consists of three parts: Header, Payload, and Signature. These sections are separated by dots.
Code Example:
Header.Payload.SignatureAnswer:
OAuth 2.0 is an authorization framework that allows third-party applications to access user resources without exposing user credentials.
Code Example:
Client
│
Authorization Server
│
Access Token
│
Resource ServerAnswer:
Bearer Token Authentication is an HTTP authentication scheme where clients send an access token in the Authorization header to access protected resources.
Code Example:
Authorization: Bearer <token>Answer:
HTTPS encrypts communication between clients and servers, protecting sensitive information such as passwords, tokens, and personal data from interception.
Code Example:
https://api.example.com/usersAnswer:
API Key Authentication uses a unique key provided by the API provider to authenticate client requests. The key is typically passed in the request header or query string.
Code Example:
GET /api/products
x-api-key: 1234567890abcdefAnswer:
Basic Authentication sends the username and password encoded using Base64 in the Authorization header. It should always be used with HTTPS.
Code Example:
Authorization: Basic dXNlcjpwYXNzd29yZA==Answer:
Use HTTPS, implement JWT or OAuth 2.0 authentication, validate user input, apply rate limiting, avoid exposing sensitive information, use proper HTTP status codes, and log security events.
Code Example:
✔ HTTPS
✔ JWT Authentication
✔ OAuth 2.0
✔ Input Validation
✔ Rate Limiting
✔ Logging