콘텐츠로 이동

← 목차

8. Streaming

streaming 다운로드

awaitDownload(sink)는 응답 본문을 버퍼링하지 않고 chunk 단위로 sink에 전달한다. 반환되는 응답은 status와 헤더를 담고 본문은 비어 있다. chunk는 받은 그대로 전달되며 content-encoding 해제를 적용하지 않는다(압축 옵션과 무관).

Files.newOutputStream(Path.of("report.bin")).use { file ->
    val response = client.get("/reports/large").awaitDownload { chunk -> file.write(chunk) }
}
  • sink는 (ByteArray) -> Unit이며 본문을 읽는 transport 스레드에서 호출된다.
  • 누적 크기는 maxResponseBodySize로 제한된다.

streaming 업로드

bodyStream(provider, contentType)는 요청 본문을 chunk 단위로 chunked transfer-encoding으로 전송한다. provider가 null을 돌려주면 본문이 끝난다.

client.post("/upload-stream")
    .bodyStream({ nextChunk() }, "application/octet-stream")
    .awaitRaw()

streaming 업로드는 provider를 rewind할 수 없으므로 자동 retry에서 제외된다 (10장).

다음: 인증과 TLS →