- What are the HTTP Methods in RESTful API and when would you use each of these?
- What does this HTTP Status codes represent?
- 1xx
- 2xx
- 3xx
- 4xx
- 5xx
- What is the difference between the following response functions?
- res.send()
- res.json()
- res.render()
- What are the appropriate status code for the following:
- Status Code OK
- Status Code Bad Request
- Status Code Unauthorized
- Status Code Forbidden
- Status Code Not Found
- Status Code Internal Server Error
-
-
Save halitbatur/ad9cc9f7c27f7207a437f7ab44c74493 to your computer and use it in GitHub Desktop.
Partners (Phamela and Sharon)
- In RESTful APIs, HTTP methods are used to perform different operations on resources. Here are
the primary methods and their typical uses:
- GET is used to retrieve data from s server and is used when we want to fetch a list of users or
getting details for a specific user. - POST is used to send data to a server to create a new resource and is used when submitting
forms or creating a new user - PUT updates an existing resource and used to update the profile of the user
- PATCH updates an existing resource partially and is used to update the email address of the user
- DELETE removes a resource from the server and is used to delete the user account
- OPTIONS describe available communications options for a resource. Check which HTTP methods
are supported for a resource (e.g, findin out if you can use GET, POST, etc, on a user resource).
- 1xx (Informational):
Purpose: Request received, continuing process.
Example: 100 Continue - Request headers received, proceed with request body.
2xx (Success):
Purpose: Request successfully received, understood, and accepted.
Example: 200 OK - Request succeeded.
3xx (Redirection):
Purpose: Further action needed to complete the request.
Example: 301 Moved Permanently - Resource permanently moved to a new URL.
4xx (Client Error):
Purpose: Client-side error, bad request.
Example: 404 Not Found - Resource not found.
5xx (Server Error):
Purpose: Server-side error, failed to fulfill a valid request.
Example: 500 Internal Server Error - Server encountered an unexpected error.
- Differences between the following responses.
res.send():
Purpose: Send a response of any type (HTML, text, JSON, etc.).
Use Case: General-purpose response sending.
res.json():
Purpose: Send a JSON response.
Use Case: Specifically for sending JSON data.
res.render():
Purpose: Render a view template.
Use Case: Generate and send HTML using a template engine.
- Status Code OK: 200
Status Code Bad Request: 400
Status Code Unauthorized: 401
Status Code Forbidden: 403
Status Code Not Found: 404
Status Code Internal Server Error: 500
@Pamela Gwala
@Yenkosii
1.GET: Retrieve data from the server.
Use: Fetch resources.
Example: GET /users (list of users), GET /users/1 (specific user).
b)POST: Submit data to create a new resource.
Use: Add new resources.
Example: POST /users (create a new user).
c)PUT: Update an existing resource completely.
Use: Replace a resource.
Example: PUT /users/1 (update user data).
d)PATCH: Update an existing resource partially.
Use: Modify part of a resource.
Example: PATCH /users/1 (update specific fields).
e)DELETE: Remove a resource.
Use: Delete resources.
Example: DELETE /users/1 (delete a user).
f)HEAD: Retrieve headers for a resource.
Use: Get metadata.
Example: HEAD /users/1 (headers for a user).
g)OPTIONS: Retrieve supported HTTP methods for a resource.
Use: Discover allowed operations.
Example: OPTIONS /users (methods supported).
h)TRACE: Echo the received request for debugging.
Use: Debug requests.
Example: TRACE /users/1 (echo request).
i)CONNECT: Establish a tunnel to the server.
Use: Create a network connection.
Exam
Example: CONNECT /users (tunnel connection).
2.1xx: Informational
100 Continue: The initial part of a request has been received, and the client should continue with the request.
101 Switching Protocols: The server is switching protocols as requested by the client (e.g., from HTTP/1.1 to WebSocket).
b)2xx: Success
200 OK: The request has succeeded.
201 Created: The request has been fulfilled, and a new resource has been created.
202 Accepted: The request has been accepted for processing, but the processing is not complete.
204 No Content: The request has been successfully processed, but there is no content to return.
c)3xx: Redirection
301 Moved Permanently: The resource has been permanently moved to a new URL.
302 Found: The resource is temporarily available at a different URL.
304 Not Modified: The resource has not been modified since the last request, so the client can use the cached version.
d)4xx: Client Errors
400 Bad Request: The server cannot or will not process the request due to client error (e.g., malformed request syntax).
401 Unauthorized: Authentication is required, and the request has not been applied because it lacks valid credentials.
403 Forbidden: The server understands the request but refuses to authorize it.
404 Not Found: The requested resource could not be found.
405 Method Not Allowed: The method specified in the request is not allowed for the resource.
409 Conflict: The request could not be completed due to a conflict with the current state of the resource.
e)5xx: Server Errors
500 Internal Server Error: The server encountered an unexpected condition that prevented it from fulfilling the request.
501 Not Implemented: The server does not support the functionality required to fulfill the request.
502 Bad Gateway: The server, while acting as a gateway or proxy, received an invalid response from an upstream server.
503 Service Unavailable: The server is currently unable to handle the request due to temporary overload or maintenance.
504 Gateway Timeout: The server, while acting as a gateway or proxy, did not receive a timely response from an upstream server.
3.a)res.send():
Purpose: Sends a JSON response.
Use Case: When you want to send a JSON object. It sets the Content-Type header to application/json automatically
b)res.json():
Purpose: Sends a JSON response.
Use Case: When you want to send a JSON object. It sets the Content-Type header to application/json automatically.
c)res.render():
Purpose: Renders a view template and sends the HTML response.
Use Case: When you want to render a view (HTML page) using a templating engine (e.g., Pug, EJS).
4.a)Status Code OK
Code: 200
Description: The request has succeeded. This is the standard response for successful HTTP requests.
b)Status Code Unauthorized
Code: 401
Description: The request requires user authentication. The client must authenticate itself to get the requested response.
Status Code Forbidden
c)Code: 403
Description: The server understands the request but refuses to authorize it. This is typically due to lack of permissions.
d)Status Code Not Found
Code: 404
Description: The server cannot find the requested resource. This status code is returned when the server cannot find what was requested
unexpected conditions.
e)Status Code Internal Server Error
Code: 500
Description: The server has encountered a situation it doesn't know how to handle. This is a generic error message for unexpected conditions.
f)status code 400 Bad Request indicates that the server cannot or will not process the request due to something perceived as a client error.