--- title: Body slug: orphaned/Web/API/Body tags: - API - BODY - Experimental - Fetch - Fetch API - Interface - NeedsTranslation - Reference - TopicStub - request translation_of: Web/API/Body original_slug: Web/API/Body ---
The Body
{{glossary("mixin")}} of the Fetch API represents the body of the response/request, allowing you to declare what its content type is and how it should be handled.
Body
is implemented by both {{domxref("Request")}} and {{domxref("Response")}}. This provides these objects with an associated body (a stream), a used flag (initially unset), and a MIME type (initially the empty byte sequence).
The example below uses a simple fetch call to grab an image and display it in an {{htmlelement("img")}} tag. You'll notice that since we are requesting an image, we need to run {{domxref("Body.blob","Body.blob()")}} ({{domxref("Response")}} implements body) to give the response its correct MIME type.
<img class="my-image" src="https://wikipedia.org/static/images/project-logos/frwiki-1.5x.png">
const myImage = document.querySelector('.my-image'); fetch('https://upload.wikimedia.org/wikipedia/commons/7/77/Delete_key1.jpg') .then(res => res.blob()) .then(res => { const objectURL = URL.createObjectURL(res); myImage.src = objectURL; });
{{ EmbedLiveSample('Examples', '100%', '250px') }}
Specification | Status | Comment |
---|---|---|
{{SpecName('Fetch','#body-mixin','Body')}} | {{Spec2('Fetch')}} |
{{Compat("api.Body")}}