{
  "id": "chunked-transfer-coding",
  "title": "Chunked transfer coding",
  "lang": "en",
  "source_lang": "en",
  "summary": "Chunked transfer coding is an HTTP/1.1 message-framing mechanism that sends a body in length-prefixed chunks without requiring its total size in advance.",
  "category": "computer-science",
  "subcategory": "networks-security",
  "type": "concept",
  "aliases": [
    "Chunked transfer encoding",
    "HTTP chunked encoding"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/chunked-transfer-coding/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/chunked-transfer-coding/",
      "json": "https://aiwiki.page/zh/chunked-transfer-coding.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "Chunked transfer coding is a message-framing mechanism in HTTP/1.1 that allows a sender to transmit a message body as a sequence of individually sized chunks rather than declare the body’s total length before transmission. Each chunk carries a hexadecimal size followed by that many data bytes. A zero-sized chunk marks the end of the data, after which optional trailer fields and a final empty line complete the message. Chunking supports incrementally generated content while preserving clear message boundaries on a reusable connection."
    },
    {
      "heading": "Purpose and scope",
      "text": "An HTTP receiver needs to determine where each message ends. This is particularly important when multiple requests and responses share a persistent TCP connection.\n\nFor an ordinary message with a body, HTTP/1.1 commonly provides three framing approaches:\n\n- A known length: a Content-Length field states the body length in bytes.\n- Chunked coding: the body is divided into explicitly sized chunks and terminated by a special final chunk.\n- Connection closure: where permitted for a response, closing the connection marks the end of its body.\n\nThese are not interchangeable in every situation. Certain responses, such as responses to HEAD requests and responses with status code 204, have no message body under HTTP’s framing rules.\n\nChunking is useful when the sender cannot conveniently determine the final length before sending headers. For example, a server may produce an HTML document while retrieving records from a database, or an API may emit results as they become available. Chunking avoids requiring the sender to buffer the entire body merely to calculate its size.\n\nAlthough sometimes called an “encoding,” chunked transfer coding does not itself perform compression, encryption, or character conversion. Its principal function is framing."
    },
    {
      "heading": "Wire format",
      "text": "A chunked message is normally identified by the header field:\n\nTransfer-Encoding: chunked\n\nEach nonfinal chunk consists of:\n\n- A chunk size, written as one or more hexadecimal digits.\n- Optional chunk extensions.\n- A carriage return followed by a line feed, conventionally written CRLF.\n- Exactly the declared number of data bytes.\n- Another CRLF.\n\nThe final chunk has a size of zero. It is followed by a trailer section, which may be empty, and an empty line terminating that section.\n\nThe following example shows a complete chunked body using visible escape sequences. Each \\r\\n represents an actual carriage-return/line-feed pair, rather than four literal characters:\n\n4\\r\\n\nWiki\\r\\n\n5\\r\\n\npedia\\r\\n\n0\\r\\n\n\\r\\n\n\nAfter chunk decoding, the body is:\n\nWikipedia\n\nThe size counts only the chunk’s data bytes, excluding its size line and surrounding delimiters. It counts bytes, not displayed characters: a character encoded using UTF-8 can occupy more than one byte.\n\nChunk data can contain arbitrary byte values, including carriage returns and line feeds. A receiver therefore locates the end of each data portion by its declared length, not by searching for a newline.\n\nA simplified structural description is:\n\nchunk-size [chunk-extensions] CRLF\nchunk-data CRLF\n...\n0 [chunk-extensions] CRLF\ntrailer-fields\nCRLF\n\nHexadecimal sizes are case-insensitive. A last-chunk size may contain multiple zero digits, although a single 0 is customary. A zero-sized chunk is a terminator, not an empty data chunk that can be followed by additional chunks."
    },
    {
      "heading": "Transfer coding versus content coding",
      "text": "HTTP distinguishes transfer coding, which concerns transferring and framing a particular message, from content coding, which describes an encoding applied to its representation.\n\nFor example:\n\nContent-Encoding: gzip\nTransfer-Encoding: chunked\n\nHere, the representation has been compressed using gzip, and the resulting compressed bytes are transmitted with chunked framing. The receiver removes the chunk framing to recover the compressed content; decompression is a separate operation.\n\nHTTP/1.1 also permits transfer-coding sequences. If chunked coding is applied to a message, it must be applied only once and must be the final transfer coding. This ensures that the receiver can determine the message boundary before reversing preceding transfer codings.\n\nChunking applies to an individual HTTP connection rather than permanently to the resource. A proxy can decode a chunked message and forward it using different chunk boundaries or another valid framing method. Consequently, chunk boundaries have no stable end-to-end meaning and should not be treated as application record boundaries."
    },
    {
      "heading": "Chunk extensions and trailer fields",
      "text": "Chunk extensions\n\nA size line can contain semicolon-prefixed extensions:\n\na;example=value\\r\\n\n\nThe hexadecimal size a specifies ten data bytes. Extensions provide a place for information associated with a particular chunk, but ordinary chunk decoding does not depend on understanding them. Receivers must ignore unrecognized chunk extensions.\n\nExtensions are not a generally reliable way to carry application information across arbitrary intermediaries: a proxy that decodes and re-encodes the message can remove or replace the original chunk structure.\n\nTrailer fields\n\nTrailer fields follow the zero-sized chunk and precede the final empty line. They can carry metadata that becomes available only after the body has been generated, such as a content digest.\n\nA sender can announce expected trailer field names in the initial header section using Trailer. In requests, the separate TE field can indicate willingness to receive trailers:\n\nTE: trailers\n\nTE and Transfer-Encoding are different fields. HTTP/1.1 recipients are required to understand chunked coding, so clients do not need to advertise TE: chunked.\n\nTrailers are subject to restrictions. Fields required for message framing, routing, authentication, or other decisions that must precede body processing are not suitable for placement in trailers. Trailer support also varies among servers, intermediaries, and application interfaces. A receiver must not indiscriminately merge trailer fields into the initial header section; doing so is appropriate only when the field’s definition permits it."
    },
    {
      "heading": "Streaming behavior and limitations",
      "text": "Chunked coding enables incremental transmission, but it does not guarantee immediate delivery to an application or user.\n\nA server can accumulate data before emitting a chunk. Network libraries, proxies, compression layers, and web browsers can introduce additional buffering. A single chunk can span several TCP segments, while multiple chunks can arrive in one read operation. Neither TCP reads nor TLS records necessarily align with chunk boundaries.\n\nPractical implications include:\n\n- Application records need their own framing. A JSON object or event can span chunks, and a chunk can contain several application records.\n- Chunk size affects overhead and buffering. Small chunks add proportionally more size-line and delimiter overhead; larger chunks may require more buffering before transmission.\n- Streaming is not exclusive to chunking. A body with a known Content-Length can also be sent and consumed incrementally.\n- Normal completion has an explicit marker. If the connection closes before the terminating structure arrives, the chunked message is incomplete.\n\nIn contrast, a response framed solely by connection closure does not provide the same explicit distinction between normal body completion and an interrupted transmission."
    },
    {
      "heading": "Security and parsing",
      "text": "Chunked coding has important security implications because different components must agree on message boundaries. When a front-end proxy and a back-end server interpret framing differently, an attacker may be able to cause HTTP request smuggling.\n\nA particularly dangerous case is a message containing both Transfer-Encoding and Content-Length. A sender must not generate this combination. Under HTTP/1.1 framing rules, Transfer-Encoding takes precedence, but receiving both can indicate an attempted framing attack. An intermediary that forwards such a message must remove the received Content-Length before forwarding it.\n\nOther parsing hazards include malformed hexadecimal sizes, missing delimiters, excessively long extensions, and numeric overflow when converting chunk sizes. An integer overflow can cause a parser to miscalculate a boundary or mishandle memory. HTTP specifications require implementations to anticipate large size values and prevent overflow or precision loss during parsing.\n\nChunking itself provides no confidentiality, authentication, or cryptographic integrity. Those properties require separate mechanisms, such as TLS and, where applicable, content-integrity fields."
    },
    {
      "heading": "Standardization and later HTTP versions",
      "text": "Chunked transfer coding was standardized as part of HTTP/1.1 in RFC 2068 in 1997 and retained in RFC 2616 in 1999. Later revisions separated HTTP’s semantics from its version-specific message syntax. RFC 7230 specified HTTP/1.1 framing in 2014; RFC 9112 replaced it in 2022 as the HTTP/1.1 messaging specification. These standards are published through the Internet Engineering Task Force.\n\nHTTP/2 and HTTP/3 do not use HTTP/1.1 chunked transfer coding. They transmit bodies in protocol-defined DATA frames and indicate completion through their stream mechanisms. Trailer fields remain possible, but are carried in trailing header sections rather than after a textual zero-sized chunk.\n\nA gateway translating between HTTP versions may therefore turn an HTTP/2 or HTTP/3 body into a chunked HTTP/1.1 body, or remove HTTP/1.1 chunking when forwarding over a newer version. The application content can remain unchanged even though its transfer framing changes."
    }
  ],
  "links": [
    "hypertext-transfer-protocol",
    "html",
    "database",
    "application-programming-interface",
    "data-compression",
    "encryption",
    "byte",
    "utf-eight",
    "metadata",
    "web-browser",
    "transport-layer-security",
    "cybersecurity",
    "integer-overflow",
    "internet-engineering-task-force"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}