Skip to main content

read_response_bounded

Function read_response_bounded 

Source
pub async fn read_response_bounded(
    response: Response,
    max_size: usize,
) -> AptosResult<Vec<u8>>
Expand description

Reads a response body with an enforced size limit, aborting early if exceeded.

Unlike response.bytes().await? which buffers the entire response in memory before any size check, this function:

  1. Pre-checks the Content-Length header (if present) to reject obviously oversized responses before reading any body data.
  2. Reads the body incrementally via chunked streaming, aborting as soon as the accumulated size exceeds max_size.

This prevents memory exhaustion from malicious servers that send huge responses (including chunked transfer-encoding without Content-Length).

ยงErrors

Returns AptosError::Api with error code RESPONSE_TOO_LARGE if the response body exceeds max_size bytes.