Response Methods
Response Methods
Section titled “Response Methods”Introduction
Section titled “Introduction”The Response object provides several methods to parse the response body in different formats.
Available Methods
Section titled “Available Methods”const response = await fetch('/api/data');
response.json(); // Parse as JSONresponse.text(); // Parse as plain textresponse.blob(); // Parse as binary blob (images, files)response.formData(); // Parse as FormDataresponse.arrayBuffer(); // Parse as ArrayBuffer
// Response propertiesresponse.status; // 200, 404, 500, etc.response.ok; // true if status 200-299response.headers; // Headers objectresponse.url; // The response URLPractical Examples
Section titled “Practical Examples”// JSON APIconst user = await fetch('/api/user/1').then(r => r.json());
// HTML or textconst html = await fetch('/template.html').then(r => r.text());
// Imageconst blob = await fetch('/image.png').then(r => r.blob());const imgUrl = URL.createObjectURL(blob);Summary
Section titled “Summary”response.json()— JSON dataresponse.text()— Plain text/HTMLresponse.blob()— Binary data (images, files)response.ok— Check for HTTP success (200-299)- Methods can only be called once (body is consumed)