23/10/2024
Làm coder cần nắm nhé anh em
🚀🚀 𝐇𝐓𝐓𝐏 𝐒𝐓𝐀𝐓𝐔𝐒 𝐂𝐎𝐃𝐄𝐒
💡 HTTP status codes are a set of standardized codes used by web servers to communicate the result of a client's request. These codes are part of the HTTP protocol and are grouped into five categories based on their general meaning:
𝟏. 𝟏𝒙𝒙 𝑰𝒏𝒇𝒐𝒓𝒎𝒂𝒕𝒊𝒐𝒏𝒂𝒍 𝑹𝒆𝒔𝒑𝒐𝒏𝒔𝒆𝒔
These status codes indicate that the server has received the request and is processing it, but no final response is available yet.
- 𝟏𝟎𝟎 𝑪𝒐𝒏𝒕𝒊𝒏𝒖𝒆: The server has received the initial part of the request, and the client should continue sending the remainder.
- 101 𝑺𝒘𝒊𝒕𝒄𝒉𝒊𝒏𝒈 𝑷𝒓𝒐𝒕𝒐𝒄𝒐𝒍𝒔: The client has requested a protocol switch, and the server is acknowledging the switch.
𝟐. 𝟐𝒙𝒙 𝑺𝒖𝒄𝒄𝒆𝒔𝒔
This category indicates that the client's request was successfully received, understood, and accepted by the server.
- 𝟮𝟬𝟬 𝗢𝗞: The request was successful, and the server has returned the requested data.
- 𝟮𝟬𝟭 𝗖𝗿𝗲𝗮𝘁𝗲𝗱: The request was successful, and a new resource has been created as a result.
- 𝟮𝟬𝟮 𝗔𝗰𝗰𝗲𝗽𝘁𝗲𝗱: The request has been accepted for processing, but it has not been completed yet.
- 𝟮𝟬𝟰 𝗡𝗼 𝗖𝗼𝗻𝘁𝗲𝗻𝘁: The server successfully processed the request, but there's no content to return.
𝟯. 𝟯𝘅𝘅 𝗥𝗲𝗱𝗶𝗿𝗲𝗰𝘁𝗶𝗼𝗻
These codes indicate that the client must take additional action to complete the request, such as following a redirect.
- 𝟯𝟬𝟭 𝗠𝗼𝘃𝗲𝗱 𝗣𝗲𝗿𝗺𝗮𝗻𝗲𝗻𝘁𝗹𝘆: The requested resource has been permanently moved to a new location.
- 𝟯𝟬𝟮 𝗙𝗼𝘂𝗻𝗱: The resource is temporarily located at a different URI, and the client should continue using the original URI for future requests.
- 𝟯𝟬𝟰 𝗡𝗼𝘁 𝗠𝗼𝗱𝗶𝗳𝗶𝗲𝗱: The resource has not been modified since the last request, so the client can use the cached version.
𝟰. 𝟰𝘅𝘅 𝗖𝗹𝗶𝗲𝗻𝘁 𝗘𝗿𝗿𝗼𝗿𝘀
These errors indicate that there was a problem with the request, often due to incorrect data or authentication failures.
- 𝟰𝟬𝟬 𝗕𝗮𝗱 𝗥𝗲𝗾𝘂𝗲𝘀𝘁: The server could not understand or process the request due to malformed syntax.
- 𝟰𝟬𝟭 𝗨𝗻𝗮𝘂𝘁𝗵𝗼𝗿𝗶𝘇𝗲𝗱: Authentication is required to access the resource, but the client has not provided valid credentials.
- 𝟰𝟬𝟯 𝗙𝗼𝗿𝗯𝗶𝗱𝗱𝗲𝗻: The server understands the request but refuses to authorize it.
- 𝟰𝟬𝟰 𝗡𝗼𝘁 𝗙𝗼𝘂𝗻𝗱: The requested resource could not be found on the server.
- 𝟰𝟬𝟱 𝗠𝗲𝘁𝗵𝗼𝗱 𝗡𝗼𝘁 𝗔𝗹𝗹𝗼𝘄𝗲𝗱: The HTTP method used in the request is not allowed for the resource.
𝟱. 𝟱𝘅𝘅 𝗦𝗲𝗿𝘃𝗲𝗿 𝗘𝗿𝗿𝗼𝗿𝘀
These status codes indicate that the server encountered an error while processing the request.
- 𝟱𝟬𝟬 𝗜𝗻𝘁𝗲𝗿𝗻𝗮𝗹 𝗦𝗲𝗿𝘃𝗲𝗿 𝗘𝗿𝗿𝗼𝗿: A generic error indicating that the server encountered an unexpected condition.
- 𝟱𝟬𝟮 𝗕𝗮𝗱 𝗚𝗮𝘁𝗲𝘄𝗮𝘆: The server, while acting as a gateway, received an invalid response from the upstream server.
- 𝟱𝟬𝟯 𝗦𝗲𝗿𝘃𝗶𝗰𝗲 𝗨𝗻𝗮𝘃𝗮𝗶𝗹𝗮𝗯𝗹𝗲: The server is temporarily unavailable, usually due to maintenance or overload.
- 𝟱𝟬𝟰 𝗚𝗮𝘁𝗲𝘄𝗮𝘆 𝗧𝗶𝗺𝗲𝗼𝘂𝘁: The server, while acting as a gateway, did not receive a timely response from the upstream server.
𝐂𝐨𝐦𝐦𝐨𝐧 𝐔𝐬𝐞 𝐂𝐚𝐬𝐞𝐬 𝐢𝐧 𝐖𝐞𝐛 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭
HTTP status codes are crucial in API development, allowing both the server and client to understand the outcome of a request and how to handle it. For instance, in RESTful APIs, a 𝟐𝟎𝟎 𝐎𝐊 is returned for successful requests, while a 𝟒𝟎𝟒 𝐍𝐨𝐭 𝐅𝐨𝐮𝐧𝐝 informs the client that the requested resource does not exist.
Proper handling of these codes is essential for building robust web applications. Developers should return appropriate status codes to reflect the state of the request, improving client-server communication and debugging processes.