<date/time> <footer> 1
HTTP Status Codes
you should know
●HTTP Request and Response journey
● Client Sends a Request:
● The client (e.g., web browser, mobile app) initiates a
request to the server for a specific resource (i.e.,URL)
● The request is sent over the network to the server.
●
● Server Receives the Request:
● The server receives the request from the client.
● The server generates a response based on the client's
request.
2
02/25/2024
●
● HTTP is stateless
3
02/25/2024
Resource/Endpoint/API
4
02/25/2024
5
02/25/2024
Best practices
6
02/25/2024
Sucees codes
7
02/25/2024
Best practices for creating RestApi (Example: user resource)
image we have a resource user we need to have CRUD operation on it? what are the
endpoints we should create and the returned status codes for calling these apis
● for create POST api/v1/users → 201 created
● for retrieving the user info GET api/v1/users/{id} → 200 ok
● for retrieving all users GET api/v1/users/ → 200 ok
● for deleting the user DELETE api/v1/users/{id} → 204 no content
● for updating the user info PUT api/v1/users/{id} → 200 ok
● for updating the user info PATCH api/v1/users/{id} → 200 ok
429 Error Too Many Requests
its a server way to say “slow down. It indicates that
the client has sent too many requests in a short time.
Use cases and examples for 4xx errors
● client sent cancel order request but per business rules you must reject it?
400 error with message indicate this state transition not allowed
● client is asking about order with specific id that is no longer exist?
404 error with message indicate the resource you are asking for is no longer exist.
● the logged in user is supervisor and tried to do operation only super admin can do?
403 forbidden indicating the user is authorized but dont have enough permission
● client is trying to create card but passed wrong or incomplete data?
400 error indicate the user input is proplimatc the to server and couldn’t continue.
what is good error to return for the following cases:
5xx Server Error Codes
500 Internal Server Error
This is the server’s cry for help. Something is
wrong on its side, and developer need to dig into
logs for clues.
502 Bad Gateway
Issues between servers, like a proxy failing to get a
response. Imagine trying to use a reverse proxy and
not getting a valid response.
503 Service Unavailable
The server can’t handle requests right now, either
due to maintenance or traffic overload.
3xx Redirection Codes and 1xx Informational Codes
301 Moved Permanently
The requested resource has moved permanently, and the
new location is provided.
302 Found (Temporary)
Similar to 301, but the original URL still works.
304 Not Modified
for Efficiency. The server confirms that the resource
hasn’t changed since it was last accessed, saving
bandwidth and time.
1xx Informational Codes
101 Switching Protocols
Used when switching from HTTP to a
WebSocket
Thank you
Any question
Basheer Almomani
Backend software engineer
basheer.almomani94@gmail.com

HTTP Status Codes you should know and use while building APIs

  • 1.
    <date/time> <footer> 1 HTTPStatus Codes you should know
  • 2.
    ●HTTP Request andResponse journey ● Client Sends a Request: ● The client (e.g., web browser, mobile app) initiates a request to the server for a specific resource (i.e.,URL) ● The request is sent over the network to the server. ● ● Server Receives the Request: ● The server receives the request from the client. ● The server generates a response based on the client's request. 2 02/25/2024
  • 3.
    ● ● HTTP isstateless 3 02/25/2024
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
    Best practices forcreating RestApi (Example: user resource) image we have a resource user we need to have CRUD operation on it? what are the endpoints we should create and the returned status codes for calling these apis ● for create POST api/v1/users → 201 created ● for retrieving the user info GET api/v1/users/{id} → 200 ok ● for retrieving all users GET api/v1/users/ → 200 ok ● for deleting the user DELETE api/v1/users/{id} → 204 no content ● for updating the user info PUT api/v1/users/{id} → 200 ok ● for updating the user info PATCH api/v1/users/{id} → 200 ok
  • 9.
    429 Error TooMany Requests its a server way to say “slow down. It indicates that the client has sent too many requests in a short time.
  • 10.
    Use cases andexamples for 4xx errors ● client sent cancel order request but per business rules you must reject it? 400 error with message indicate this state transition not allowed ● client is asking about order with specific id that is no longer exist? 404 error with message indicate the resource you are asking for is no longer exist. ● the logged in user is supervisor and tried to do operation only super admin can do? 403 forbidden indicating the user is authorized but dont have enough permission ● client is trying to create card but passed wrong or incomplete data? 400 error indicate the user input is proplimatc the to server and couldn’t continue. what is good error to return for the following cases:
  • 11.
    5xx Server ErrorCodes 500 Internal Server Error This is the server’s cry for help. Something is wrong on its side, and developer need to dig into logs for clues. 502 Bad Gateway Issues between servers, like a proxy failing to get a response. Imagine trying to use a reverse proxy and not getting a valid response. 503 Service Unavailable The server can’t handle requests right now, either due to maintenance or traffic overload.
  • 12.
    3xx Redirection Codesand 1xx Informational Codes 301 Moved Permanently The requested resource has moved permanently, and the new location is provided. 302 Found (Temporary) Similar to 301, but the original URL still works. 304 Not Modified for Efficiency. The server confirms that the resource hasn’t changed since it was last accessed, saving bandwidth and time. 1xx Informational Codes 101 Switching Protocols Used when switching from HTTP to a WebSocket
  • 13.
    Thank you Any question BasheerAlmomani Backend software engineer basheer.almomani94@gmail.com