본문 바로가기
👩🏻‍💻 Front-end/🌐 Web

Content-Length can't be present with Transfer-Encoding 에러 원인

by su-no 2024. 5. 2.

Content-Length와 Transfer-Encoding: chunked의 차이

  • Content-Length: HTTP 응답 본문의 정확한 크기를 나타내는 헤더입니다. 서버는 응답 본문의 크기를 계산하여 이 헤더에 포함시킵니다.
  • Transfer-Encoding: chunked: 응답 본문이 청크 단위로 전송됨을 나타내는 헤더입니다. 청크 크기는 각 청크의 시작 부분에 명시됩니다. 이 방식을 사용하면 응답 본문의 크기를 미리 알 필요가 없습니다.

문제 발생 원인

  • HTTP 1.1 명세에 따르면 Content-LengthTransfer-Encoding: chunked 헤더가 동시에 존재하는 것은 허용되지 않습니다. [1]
  • 이는 서버가 응답을 보내는 방식이 명확하지 않아 클라이언트 입장에서 혼란을 겪을 수 있기 때문입니다. [2]

문제 해결 방법

  • 서버 측에서는 Content-Length 헤더와 Transfer-Encoding: chunked 헤더 중 하나만 사용해야 합니다.
  • 일반적으로 Transfer-Encoding: chunked를 사용하는 것이 더 권장됩니다. 이 경우 Content-Length 헤더는 생략할 수 있습니다.
  • 클라이언트 측에서도 이 두 헤더가 동시에 존재하는 경우 적절히 처리해야 합니다.

추가 정보: HTTP 1.1의 Content-Length 헤더

  • HTTP 1.1에서는 Content-Length 헤더를 사용하여 응답 본문의 크기를 나타냅니다.
  • 이 헤더를 사용하면 클라이언트가 응답 본문의 크기를 미리 알 수 있어 효율적인 처리가 가능합니다.
  • 하지만 응답 본문의 크기를 미리 알 수 없는 경우에는 Transfer-Encoding: chunked를 사용하는 것이 더 적합합니다. [4]

참고 자료

[1] MDN Web Docs - Transfer-Encoding - HTTP - MDN Web Docs (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding)
[2] GitHub - Help, Parse Error: Content-Length can't be present with ... (https://github.com/sindresorhus/got/discussions/1576)
[3] 윤복로그 - transfer encoding ( chunked , content-length) - 윤복로그 (https://goyunji.tistory.com/8)
[4] GitHub - http/1.1 protocol error: both 'Content-Length' and 'Transfer- ... (https://github.com/envoyproxy/envoy/issues/14004)
[5] MDN Web Docs - Transfer-Encoding - HTTP - MDN Web Docs (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding)
[6] Stack Overflow - Chunked encoding and content-length header (https://stackoverflow.com/questions/3304126/chunked-encoding-and-content-length-header)
[7] 윤복로그 - transfer encoding ( chunked , content-length) - 윤복로그 (https://goyunji.tistory.com/8)
[8] 티스토리 - [HTTP] Transfer-Encoding - Be OK - 티스토리 (https://sg-choi.tistory.com/631)