Understanding Fetch API Response Methods and the Content-Type Header

Anatolii Yatsenko
4 min readFeb 21, 2024

--

What?

When building web applications, developers often need to request and retrieve data from servers. The Fetch API is a modern interface that allows browsers to request data. It comes with various response methods linked to different data types, ensuring efficient data handling and processing. It’s essential to understand how these methods work and the role of the Content-type header in ensuring the correct data exchange between the client and server.

Why?

The Content-type header plays a crucial role in web communication by specifying the type of data being sent or received. This header helps the client and server understand what data is being exchanged, ensuring that the data is handled and parsed correctly. It's essential for both the client sending requests and the server sending responses to set this header appropriately to prevent errors and ensure data integrity.

Key Response Methods of the Fetch API

The Fetch API provides several methods to handle the returned data, depending on its format:

  • text() for plain text data.
  • blob() for binary data (like images and videos).
  • json() for JSON format data.
  • arrayBuffer() for handling binary data in a low-level, manual format.
  • formData() for handling forms and file uploads.
  • clone() to duplicate the response.

These methods help parse the response data into a usable and readable format in you code.

Practical Examples and MIME Types

Different types of data require different handlings, as specified by MIME (Multipurpose Internet Mail Extensions) types. Here’s a brief overview of how to use Fetch API methods with various most-used by developers MIME types:

  • Application Data: Use blob() for binary data like PDFs (application/pdf) or json() for JSON data (application/json).
  • Images: Use blob() for image formats like PNG (image/png) or JPEG (image/jpeg).
  • Audio and Video: Use blob() for files like MP3s (audio/mpeg) or MP4s (video/mp4).
  • Text: Use text() for plaintext (text/plain), HTML (text/html), or CSS (text/css).

The full list of mime-types you can find by following the link:

https://www.iana.org/assignments/media-types/media-types.xhtml

Detailed MIME Types and Fetch API Response Methods

This list provides a clear roadmap for developers to handle various data types effectively using the Fetch API, ensuring that applications can process and present data accurately and efficiently.

You can use it to write helper function which based on the Content-type header will use appropriate Response instance method when using Fetch API or use this list in any other architecture and optimization related process based on your needs.

Application types

  • Generic binary data (application/octet-stream): Use blob().
  • Java archives (application/java-archive): Use blob().
  • EDI data interchange (application/EDI-X12): Use text() or blob().
  • JavaScript files (application/javascript): Use text() or blob().
  • XML data (application/xml): Use text() or blob().
  • PDF documents (application/pdf): Use blob().
  • Ogg multimedia files (application/ogg): Use blob().
  • ZIP archives (application/zip): Use blob().
  • XHTML documents (application/xhtml+xml): Use text() or blob().
  • Adobe Flash files (application/x-shockwave-flash): Use blob().
  • JSON data (application/json): Use json().
  • Form data (application/x-www-form-urlencoded): Use text() or manual parsing.
  • JSON-LD data (application/ld+json): Use json().
  • EDI data interchange (application/EDIFACT): Use text() or blob().

Audio types

  • MPEG audio files (audio/mpeg): Use blob().
  • RealAudio files (audio/vnd.rn-realaudio): Use blob().
  • WAV files (audio/x-wav): Use blob().
  • Windows Media Audio files (audio/x-ms-wma): Use blob().

Font types

  • WOFF and WOFF2 fonts (application/font-woff, application/font-woff2): Use blob().
  • EOT fonts (application/vnd.ms-fontobject): Use blob().
  • OpenType and TrueType fonts (application/x-font-opentype, application/x-font-truetype, application/x-font-ttf): Use blob().
  • Font collections and formats (font/collection, font/otf, font/sfnt, font/ttf, font/woff, font/woff2): Use blob().

Image types

  • GIF, TIFF, JPEG, PNG, ICO, and other image formats (image/gif, image/tiff, image/jpeg, image/png, image/x-icon, image/vnd.microsoft.icon): Use blob().
  • SVG files (image/svg+xml): Use text() or blob() depending on content.
  • DJVU files (image/vnd.djvu): Use blob().

Model types

  • GLTF and Collada models (model/gltf+json, model/gltf-binary, model/vnd.collada+xml): Use json() or blob().
  • OBJ and STL files (model/obj, model/x.stl-binary): Use text() or blob().

Text types

  • CSS, HTML, XML, CSV, and plain text files (text/css, text/html, text/xml, text/csv, text/plain): Use text().

Video types

  • MPEG, WMV, AVI, WebM, MP4, FLV, and QuickTime videos (video/mpeg, video/x-ms-wmv, video/x-msvideo, video/webm, video/mp4, video/x-flv, video/quicktime): Use blob().

Multipart types

  • Form submissions with files (multipart/form-data): Use formData().
  • Multipart responses with related content (multipart/related): Use blob().

Summary

The Fetch API provides a versatile set of methods for handling different types of response data, with the Content-type header playing a critical role in specifying the data type being exchanged. Understanding these methods and how to apply them based on the MIME type of the data ensures efficient data handling, parsing, and overall better performance of web applications and code readability and maintainability.
Hope this article was useful for you!

--

--