Response.bodyUsed

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

bodyUsedResponse インターフェイスの読み取り専用プロパティで、本体が読み取り済みであるかどうかを示す論理値です。

論理値です。

フェッチリクエストの例フェッチリクエストをライブで実行)では、 Request() コンストラクターを使用して新しいリクエストを作成し、それを使用して JPG を読み取ります。読み取りが成功したら、blob() を使用してレスポンスから Blob を読み取り、URL.createObjectURL を使用してオブジェクト URL に格納し、その URL を <img> 要素のソースとして設定して画像を表示します。

response.bodyUsedresponse.blob() の呼び出し前後にコンソールに記録していることに注目してください。 その時点で本文が読み取られたかによるため、これは呼び出し前では false を返し、その後では true を返します。

HTML コンテンツ

html
<img
  class="my-image"
  src="https://wikipedia.org/static/images/project-logos/frwiki-1.5x.png" />

JS コンテンツ

js
const myImage = document.querySelector(".my-image");
fetch("https://upload.wikimedia.org/wikipedia/commons/7/77/Delete_key1.jpg")
  .then((response) => {
    console.log(response.bodyUsed);
    const res = response.blob();
    console.log(response.bodyUsed);
    return res;
  })
  .then((response) => {
    const objectURL = URL.createObjectURL(response);
    myImage.src = objectURL;
  });

結果

仕様書

Specification
Fetch Standard
# ref-for-dom-body-bodyused①

ブラウザーの互換性

BCD tables only load in the browser

関連情報