Course contentsShow
← Web Development
Lesson 829 of 2,98215. Browser NetworkingFree lesson

Response Body Methods: json(), text(), blob(), and More

Master different methods to extract response data including json(), text(), blob(), arrayBuffer(), and formData().

Response Body Methods: json(), text(), blob(), and More

You'll learn how to extract different data formats from fetch responses using the appropriate body methods.

The Core Idea

When you fetch a resource from a server, the Response object contains the data, but you need to explicitly tell JavaScript how to read that body. The Response object provides specialized methods that parse the body into different formats: json() for JSON data, text() for plain text, blob() for binary files, and more.

Think of it like receiving a sealed package—you know something's inside, but you need the right tool to open it depending on what you ordered: scissors for a box, a letter opener for an envelope, etc.

The Body Methods

Each method returns a Promise that resolves with the parsed data:

  • json() - Parses the body as JSON and returns a JavaScript object
  • text() - Returns the body as a plain text string
  • blob() - Returns a Blob object for binary data (images, videos, files)
  • arrayBuffer() - Returns raw binary data as an ArrayBuffer
  • formData() - Parses the body as form data

Usage Pattern

fetch('https://api.example.com/data')
  .then(response => response.json())  // Choose the right method
  .then(data => {
    console.log(data);  // Now you have a JavaScript object
  });

fetch('https://example.com/page.html')
  .then(response => response.text())  // Get HTML as string
  .then(html => console.log(html));

fetch('https://example.com/photo.jpg')
  .then(response => response.blob())  // Get image as Blob
  .then(blob => {
    const imageUrl = URL.createObjectURL(blob);
  });

Important Constraint

You can only read a response body once. Calling multiple methods on the same response will fail because the stream is consumed.

Key Takeaway: Use the appropriate Response body method based on your data format—json() for APIs, text() for HTML/plain text, blob() for files, arrayBuffer() for raw binary, and formData() for form submissions.