Skip to content

Instantly share code, notes, and snippets.

@halitbatur
Created June 25, 2024 06:47
Show Gist options
  • Save halitbatur/ad9cc9f7c27f7207a437f7ab44c74493 to your computer and use it in GitHub Desktop.
Save halitbatur/ad9cc9f7c27f7207a437f7ab44c74493 to your computer and use it in GitHub Desktop.
Rest Discussion

RESTful API Discussion Questions

  1. What are the HTTP Methods in RESTful API and when would you use each of these?
  2. What does this HTTP Status codes represent?
    • 1xx
    • 2xx
    • 3xx
    • 4xx
    • 5xx
  3. What is the difference between the following response functions?
    • res.send()
    • res.json()
    • res.render()
  4. 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
@Tumelo2748
Copy link

@Tumelo2748
@MissAngelaKing
@Skosanalindo061

  1. The methods use for Http in RESTful API
  • GET: Retrieve a resource (e.g., fetch a user's data)
  • POST: Create a new @resource (e.g., create a new user account)
  • PUT: Update an existing resource (e.g., update a user's profile)
  • DELETE: Delete a resource (e.g., delete a user account)
  • PATCH: Partially update an existing resource (e.g., update a user's email address)
  • OPTIONS: Return the HTTP methods supported by a resource
  • HEAD: Return metadata about a resource (e.g., headers only)
  • 1xx: Informational responses. These codes indicate that the request has been received and the server is processing it.
  • 2xx: Success responses. These codes indicate that the request was successful and the server has returned the requested data.
  • 3xx: Redirection responses. These codes indicate that the resource has been moved to a new URL or the server wants the client to perform an additional action.
  • 4xx: Client error responses. These codes indicate that the client sent an invalid request or cannot access the resource.
  • 5xx: Server error responses. These codes indicate that the server encountered an error and cannot fulfill the request.
  1. Response Functions:
  • res.send(): Send a response with a string or buffer body
  • res.json(): Send a response with a JSON body
  • res.render(): Render a template and send the rendered HTML as the response
  • OK: 200. Use when the request was successful and the server has returned the requested data.
  • Bad Request: 400. Use when the client sent an invalid request or the server cannot process it.
  • Unauthorized: 401. Use when the client is not authenticated or does not have valid credentials.
  • Forbidden: 403. Use when the client is authenticated but does not have permission to access the resource.
  • Not Found: 404. Use when the requested resource does not exist on the server.
  • Internal Server Error: 500. Use when the server encountered an unexpected error and cannot fulfill the request

@NtokozoMitchell
Copy link

@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.

@PhamelaMhlaba
Copy link

Partners (Phamela and Sharon)

  1. 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).
  1. 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.

  1. 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.

  1. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment