Cette traduction est incomplète. Aidez à traduire cet article depuis l'anglais.

The Body 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 Request and 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).

Properties

Body.body Lecture seule
A simple getter used to expose a ReadableStream of the body contents.
Body.bodyUsed Lecture seule
A Boolean that indicates whether the body has been read.

Methods

Body.arrayBuffer()
Takes a Response stream and reads it to completion. It returns a promise that resolves with an ArrayBuffer.
Body.blob()
Takes a Response stream and reads it to completion. It returns a promise that resolves with a Blob.
Body.formData()
Takes a Response stream and reads it to completion. It returns a promise that resolves with a FormData object.
Body.json()
Takes a Response stream and reads it to completion. It returns a promise that resolves with a JSON object.
Body.text()
Takes a Response stream and reads it to completion. It returns a promise that resolves with a USVString (text). The response is always decoded using UTF-8.

Examples

The example below uses a simple fetch call to grab an image and display it in an <img> tag. You'll notice that since we are requesting an image, we need to run Body.blob() (Response implements body) to give the response its correct MIME type.

HTML Content

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

JS Content

var myImage = document.querySelector('.my-image');
fetch('https://upload.wikimedia.org/wikipedia/commons/7/77/Delete_key1.jpg')
	.then(res => res.blob())
	.then(res => {
		var objectURL = URL.createObjectURL(res);
		myImage.src = objectURL;
});

Specifications

Specification Status Comment
Fetch
La définition de 'Body' dans cette spécification.
Standard évolutif  

Browser compatibility

Nous convertissons les données de compatibilité dans un format JSON. Ce tableau de compatibilité utilise encore l'ancien format car nous n'avons pas encore converti les données qu'il contient. Vous pouvez nous aider en contribuant !

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 42
 
(Oui) 39 (39) [1] Pas de support

29

Pas de support
body as a ReadableStream 43 ? Pas de support[2] Pas de support 30 Pas de support
formData() method 60 ? ? Pas de support 47 Pas de support
Feature Android Webview Chrome for Android Edge Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 42 42 (Oui) Pas de support Pas de support Pas de support

29

Pas de support
body as a ReadableStream 43 43 ? Pas de support[2] Pas de support Pas de support

30

Pas de support
formData() method 60 60 ? Pas de support Pas de support Pas de support

47

Pas de support

[1] Behind a preference starting with version 34.

[2] Readable streams are currently enabled in Firefox, but hidden behind the dom.streams.enabled and javascript.options.streams prefs.

See also

 

Étiquettes et contributeurs liés au document

Contributeurs à cette page : vqrs
Dernière mise à jour par : vqrs,