HTTP 상태 코드

wldbs._.·2024년 12월 4일

WEB-STUDY

목록 보기
5/9
post-thumbnail

Hypertext Transfer Protocol (HTTP) Status Code Registry
“Top 10” HTTP Status Code explained you need to know as a Front End Developer
WIKIPEDIA: List of HTTP status codes
mdn web docs: HTTP 상태 코드
혼공: HTTP 상태 코드 전체 요약
🌐 HTTP 상태 코드(1XX ~ 5XX) 종류 총정리
CHATGPT

자주 쓰이는 HTTP 상태 코드를 알아보자.


1. 상태 코드란?

응답 코드는 클라이언트(브라우저 등)의 요청에 대한 서버의 결과를 나타내는 3자리 숫자이다.
각 상태 코드는 숫자 범위에 따라 특정 카테고리에 속하며, 클라이언트와 서버 간의 의사소통을 명확히 한다.
404 Not Found와 같이 응답 코드와 응답 결과에 대한 이유를 함께 클라이언트로 전달한다.

  • 응답 결과에 대한 이유는 권장 사항이기 때문에 서버에서 전달 받은 문구가 아닌,
    클라이언트에서 서비스에 따라 적당한 문구를 사용자에서 얼마든지 제공할 수 있다.

응답 코드는 아래와 같이 크게 5가지로 그룹 지을 수 있다.

  • 1xx (응답 정보): 요청이 접수되어, 요청이 진행 중임
  • 2Xx (성공): 요청이 성공적으로 수행되었음
  • 3xx (리다이렉션): 요청을 완료하려면 추가 조치가 필요함
  • 4xx (클라이언트 에러): 클라이언트의 요청에 잘못된 내용이 포함되어 있어 요청을 수행할 수 없음
  • 5xx (서버 에러): 올바른 요청을 보냈지만 서버가 수행하지 못했음

These codes are included in the response header and provide information about the outcome of the request.

  • The first digit of the status code indicates the general class of response,
  • while the last two digits provide more specific information about the response.

1-1. 성공 코드

프로젝트에서는 200201, 204가 자주 쓰인다.


200 OK

요청이 성공적으로 되었다.

성공의 의미는 HTTP 메소드에 따라 달라진다.

  • GET: 리소스를 불러와서 메시지 바디에 전송되었다. → 데이터 반환 성공
  • PUT 또는 POST: 수행 결과에 대한 리소스가 메시지 바디에 전송되었다. → 서버가 요청 데이터를 성공적으로 처리

HTTP status code 200 is a success status code,
which indicates that the request was successful and the server has returned the requested resource in the response.
In other words, this code indicates that the server was able to process the request and provide the client with the requested information.


201 Created

요청이 성공적이었으며, 그 결과로 새로운 리소스가 생성되었다.

이 응답은 일반적으로 POST 요청 또는 일부 PUT 요청 이후에 따라온다.

  • PUT 또는 POST ← 데이터 생성 시 사용

HTTP status code 201 is a success status code
that indicates that a new resource has been successfully created on the server
in response to a POST or PUT request.
It is often used in RESTful APIs to signal the successful creation of a new resource.

It is worth noting that a 201 status code should only be returned when a new resource is created successfully,
and not when an existing resource is updated.
If an existing resource is updated successfully, the server should return a 200 OK status code instead.


204 No Content

요청이 성공적으로 처리되었지만, 응답 본문(body)에 반환할 데이터가 없다.

헤더만 포함된 간결한 응답을 반환한다.
요청에 대해서 보내줄 수 있는 콘텐츠가 없지만, 헤더는 의미있을 수 있다.

  • DELETE: 특정 데이터를 성공적으로 삭제한 경우, 반환할 내용이 없으므로 204를 응답
  • PUT, PATCH: 특정 데이터를 업데이트했으나, 새로운 데이터를 반환하지 않는 경우

HTTP status code 204 is a success status code
that indicates that the server has successfully processed the request,
but does not need to return a response body.
In other words, the server has fulfilled the request, but there is no content to send back to the client.

For example, a client might send a DELETE request to the server to delete a resource,
and the server might respond with a 204 status code to indicate
that the resource has been successfully deleted, but there is no content to return.


1-2. 리다이렉션 코드

프로젝트에서는 301302가 자주 쓰인다.
301 Moved Permanently302 Found는 모두 클라이언트를 다른 URL로 리다이렉트하기 위한 HTTP 상태 코드이다.
둘의 차이는 리다이렉션의 영구성에 있다.


301 Moved Permanently

리소스의 위치가 영구적으로 변경되었다.

이 응답 코드는 요청한 리소스의 위치가 변경되었음을 의미한다.

  • URL 구조 변경 시
  • 웹사이트 도메인을 이전하거나 페이지 경로를 재구성할 때

클라이언트의 동작:

  • 브라우저는 새로운 URL로 자동 이동
  • 이후 동일한 요청을 할 때도 새 URL을 사용 (검색 엔진도 새 URL을 기록)

This and all future requests should be directed to the given URL.


302 Found (Previously "Moved temporarily")

요청한 리소스가 임시적으로 다른 위치로 이동되었다.

이 응답 코드는 요청한 리소스의 URI가 일시적으로 변경되었음을 의미한다.

  • 유지보수 중 임시 페이지로 리다이렉션
  • 임시적으로 URL을 변경할 때
    → 리소스의 원래 URL은 유효하며, 검색 엔진이 새 URL을 인덱싱하지 않음

클라이언트의 동작:

  • 그러므로, 클라이언트는 향후의 요청도 반드시 동일한 URL로 해야한다.

301: "이 페이지는 여기로 옮겨졌습니다. 앞으로 이 URL로 가세요."
302: "잠시 다른 곳으로 안내합니다. 하지만 이 URL이 여전히 유효합니다."


1-3. 클라이언트 오류 코드

프로젝트에서는 400, 401, 403, 404가 자주 쓰인다.


400 Bad Request

요청이 잘못되어 서버가 처리할 수 없다.

이 응답은 잘못된 문법으로 인하여 서버가 요청을 이해할 수 없음을 의미한다.

  • 잘못된 JSON 형식, 필수 파라미터 누락 등

HTTP status code 400 is a client error status code
that indicates that the server was unable to process the client’s request
because the request was malformed or invalid in some way.

When a server returns a 400 status code,
it will typically include a response body that provides additional information about the error,
such as a description of the problem and suggestions for how to correct it.
This information can help the client to understand what went wrong with the request and how to fix it.


401 Unauthorized

인증이 필요하지만 제공되지 않았거나 잘못되었다.

의미상 이 응답은 "비인증(unauthenticated)"을 의미한다.
클라이언트는 요청한 응답을 받기 위해서는 반드시 스스로를 인증해야 한다.

HTTP status code 401 is an authentication error response code in the HTTP protocol.
It means that the client making the request is not authorized to access the resource requested.

In other words, when a user tries to access a protected resource such as a webpage or file,
the server will require the user to provide valid credentials such as a username and password.


403 Forbidden

요청은 이해했으나, 권한이 없어 거부한다.

클라이언트는 콘텐츠에 접근할 권리를 가지고 있지 않다.
예를 들어, 그들은 미승인이어서 서버는 거절을 위한 적절한 응답을 보낸다.

HTTP status code 403 is a client error status code
that indicates that the server understood the client’s request, but refuses to fulfill it.
This can happen for a variety of reasons,
such as when the client does not have sufficient permissions to access the requested resource,
or when the requested resource is restricted or forbidden.

401과 다른 점은 서버가 클라이언트가 누구인지 알고 있다.

It’s important to note that a 403 status code is different from a 401 status code,
which is used to indicate that the client needs to authenticate or provide credentials before accessing the requested resource.
In contrast, a 403 status code indicates that the client is authenticated, but does not have sufficient permissions to access the requested resource.


404 Not Found

요청한 리소스를 찾을 수 없다.

서버는 요청받은 리소스를 찾을 수 없다.
브라우저에서는 알려지지 않은 URL을 의미한다.

  • 잘못된 URL 또는 존재하지 않는 리소스를 요청했을 때 발생

HTTP status code 404 is a client error status code
that indicates that the server was unable to find the requested resource or document.
This can happen for a variety of reasons,
such as when the URL for the resource is incorrect or when the resource has been removed or deleted from the server.

In addition to indicating that the requested resource was not found,
a 404 status code can also be used to indicate
that a server does not wish to reveal whether a resource exists or not, for security or privacy reasons.


1-4. 서버 오류 코드

프로젝트에서는 500503이 자주 쓰인다.


500 Internal Server Error

서버에서 요청 처리 중 예상치 못한 오류가 발생했다.

서버 코드에서 처리 방법을 모르는 상황, 즉 예외가 발생했을 때 주로 반환한다.
서버는 아직 처리 방법을 알 수 없다.

In other words, when a user sends a request to a server, the server may encounter an internal error
that prevents it from processing the request as expected.
When this happens, the server will return an HTTP 500 status code
along with an error message to indicate that something went wrong on the server side.

Some common causes of HTTP 500 errors include problems
with server configurations, programming errors in server-side scripts or applications,
database connection errors, or insufficient server resources like memory, disk space, or processing power.


503 Service Unavailable

서버가 일시적으로 사용할 수 없다.

서버가 요청을 처리할 준비가 되지 않았다.
일반적인 원인은 유지보수를 위해 작동이 중단되거나 과부하가 걸렸을 때이다.


2. 결론

HTTP status codes provide a standardized way for servers
to communicate with clients about the outcome of HTTP requests,
which helps clients to understand what happened and take appropriate action.

profile
공부 기록용 24.08.05~ #LLM #RAG

0개의 댓글