코규리
article thumbnail

 

문제사항


Get 요청이 들어왔다

요청을 보내는 path까지는 알맞으나, 데이터가 없으면 어떤 코드를 내보내야할까?

http://dblab112.org/request/member?name=gimgu
  1. 200, 접근 url자체는 알맞으니 요청성공(200)을 안내하고 Empty body를 반환한다
  2. 400, 잘못된 요청임을 표시한다
  3. 404, 리소스가 잘못된 요청임을 표시한다

400 vs 404 에 대한 의견이 분분하다

 

 

200 OK + Empty Body


The HTTP **200 OK** success status response code indicates that the request has succeeded. A 200 response is cacheable by default.

  • 200(ok)는 GET, POST에 주로 사용한다
  • PUT, DELETE 처럼 처리 후 데이터를 보내줄 필요가 없다면 204(No Content)를 사용한다
    • 204: 추가로 전달한 데이터가 일반적으로 없을 경우
    • query string에 따라 데이터의 유무가 판별날 때 말고, 데이터가 없는 걸 의도로 설계되었을 때.

 

400 NotFound


The HyperText Transfer Protocol (HTTP) 400 Bad Request  response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request message framing, or deceptive request routing).

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

  • 클라이언트에 의한 오류다 (잘못된 요청구문, 잘못된 요청)
  • 기괴한(?) 요청문법, 타당하지 않은 요청 메시지 배치, 그럴듯하지만 잘못된 라우팅 요청
  • 요청하는 path에 대한 오류라는 이미지가 강해보인다 (개인적인 의견)


404 Bad Request


The HTTP 404 Not Found  response status code indicates that the server cannot find the requested resource. Links that lead to a 404 page are often called broken or dead links and can be subject to link rot . A 404 status code only indicates that the resource is missing: not whether the absence is temporary or permanent. If a resource is permanently removed, use the [410](<https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/410>)  (Gone) status instead.

  • 서버가 요청받은 리소스를 찾을 수 없습니다
  • Broken Link, Dead Link, link not
  • 리소스가 영구적으로 삭제되었다면 410 상태 코드가 쓰여야 함
  • 여기서 말하는 리소스가 ‘Data’까지 의미하느냐, ‘uri’ 를 의미하느냐 에서 의견이 갈린다
    • request/member 까지만 리소스로 인정한다면 다른 응답상태코드를 찾을 것이다
    • request/member?name=gimgu 전체를 리소스로 인정한다면 404로 지정할 것이다

 

결론


  • Delete, Put API의 경우 204 (알맞은 요청, 반환데이터는 없을 때) 를 고려해보자
  • 애매한 예외상황에 대해서는 400(Bad Request)로 처리를 하는 것이 일반적이라 하지만, 그 중에서도 요청 문법에 대한 오류라면 400이 특히 알맞아보인다.
  • 개인적으로, 404가 이 상황에서 가장 알맞은 것으로 보인다
  • 501(Not Implemented)도 고려할 수 있다

별도의 자세한 메시지 제공으로 해당 예외에 대한 표시가 필요하다

  • 파라미터 위치, 사용자 입력 값 등의 오류 명시
HTTP/1.1 404 Not Found
{
	"errors": [
		{
			"location": "body",
			"param": "name",
			"value": "gimgu,
			"error": "NOT EXISTS",
			"msg": "input NOT EXISTS Data"
		}
	]
}
HTTP/1.1 404 Not Found
{
	"message" : "Acces Not exists data, input 'name': 'gimgu' "
}

 

 

 

 

REFER


 

400 Bad Request - HTTP | MDN

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (for example, malformed request syntax, invalid request mes

developer.mozilla.org

 

RFC 7231 - Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content

 

datatracker.ietf.org

 

404 Not Found or Bad Request?

Let's say that we have the following REST call: GET api/companies/5 (get company with id 5) If company '5' doesn't exist, we would typically return a 404 Not Found response. But now, let's tak...

stackoverflow.com

 

400 BAD REQUEST vs 404 NOT FOUND

404 NOT FOUND 레벨2 기간 미션을 진행하던 중 크로플이 도저히 이유를 알 수 없는 테스트 에러를 마주쳐서, 도와줄 사람을 찾고 있다며 찾아왔다. 이 때 크로플이 대략 아래와 같은 테스트 코드를

hyeon9mak.github.io

 

REST API 관점에서 바라보는 HTTP 상태 코드(HTTP status code)

<!DOCTYPE html> REST API 관점에서 바라보는 HTTP 상태 코드(HTTP status code) REST API 관점에서 바라보는 HTTP 상태 코드(HTTP status code) TOC Introduction HTTP 와 REST HTTP Status Code 2XX Success 4...

sanghaklee.tistory.com