Skip to content

Why does my download handler say the response body was already used after I logged it?

A Fetch Response body is normally a one-use stream. If diagnostic code calls text(), json(), blob() or another reader first, the download path cannot simply read it again. Trace the consumer and give the body one bounded handling pipeline. Do not request a new generation to obtain another response, and do not treat response.clone() as an unlimited, cost-free logging solution.

SHIP A RELIABLE INTEGRATIONAISwapLab team · Editorial methodPublished 3 min read
Open API documentationExplore 67 Image SkillsPrivate keys · Explicit credit approval
Article workflow: Locate the first reader; Choose one deliberate consumption path; Test ownership rather than adding retries.
Workflow diagram: Locate the first reader → Choose one deliberate consumption path → Test ownership rather than adding retries. Read the article for evidence and limits.

A successful response can be drained before delivery

MDN's bodyUsed property reports whether the body has been read; changing a reference to that object does not rewind its bytes. clone() must be called before consumption. Its two branches can also buffer unread data without a bound when their consumption speeds differ. This is a client stream-ownership issue, separate from validating HTTP status or cleaning up an abandoned transfer. It does not establish a defect in AISwapLab's content service.

What to do next

  1. 01

    Locate the first reader

    Follow the returned Response through middleware, error reporting and the actual download function. Look for convenience logging that reads the entire body, a decoder that runs before the media branch, or a reader left locked. Observe bodyUsed and the control path without dumping private content. Retain status, expected kind and the known job ID before body processing so a secondary read error does not erase useful context.

  2. 02

    Choose one deliberate consumption path

    For a small bounded JSON response, read and validate it once, then pass the reviewed parsed value to other components. For media, use a bounded staging or streaming pipeline and record approved metadata instead of reading the whole image or video for logs. If a controlled small response genuinely requires cloning, create the clone before any read and account for both consumers. Do not clone large private media into a slow or unused diagnostic branch; that can defeat the intended memory bound.

  3. 03

    Test ownership rather than adding retries

    Use a synthetic local response and a logging wrapper that consumes it first. Verify that the corrected design delivers the expected bytes or parsed object through one owner, and that failures release readers and partial files. Test the error branch as well as successful media. If the existing completed asset needs retrieval again after a local failure, refetch its authorized content route under normal validation; keep generation and download recovery separate.

A concrete example

Illustrative failure: a wrapper awaits response.text() to print a media response, then hands that same Response to code calling blob(). The second reader fails because the body was consumed. The proposed correction records safe status/type metadata and transfers the bytes once into the validated delivery path. This is a local fixture design, not an executed test or a reason to log real customer footage.

Before you call it ready

  • Every Response body has a known consuming owner.
  • Diagnostic code does not silently drain, clone or retain a large private body.
  • The local failure remains attached to the existing completed job.

When to stop or choose another method

Stop adding another paid request or an unconditional clone when stream ownership is unclear. Fix the reader sequence and resource policy; bodyUsed is evidence of consumption, not a reset button.

Sources

One task. A recoverable workflow.

Check the contract, approve the quote and retain the job.

Open API documentation