Response - Web APIs | MDN (2024)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

The Response interface of the Fetch API represents the response to a request.

You can create a new Response object using the Response() constructor, but you are more likely to encounter a Response object being returned as the result of another API operation—for example, a service worker FetchEvent.respondWith, or a simple fetch().

Constructor

Response()

Creates a new Response object.

Instance properties

Response.body Read only

A ReadableStream of the body contents.

Response.bodyUsed Read only

Stores a boolean value that declares whether the body has been used in a response yet.

Response.headers Read only

The Headers object associated with the response.

Response.ok Read only

A boolean indicating whether the response was successful (status in the range 200299) or not.

Response.redirected Read only

Indicates whether or not the response is the result of a redirect (that is, its URL list has more than one entry).

Response.status Read only

The status code of the response. (This will be 200 for a success).

Response.statusText Read only

The status message corresponding to the status code. (e.g., OK for 200).

Response.type Read only

The type of the response (e.g., basic, cors).

Response.url Read only

The URL of the response.

Static methods

Response.error()

Returns a new Response object associated with a network error.

Response.redirect()

Returns a new response with a different URL.

Response.json()

Returns a new Response object for returning the provided JSON encoded data.

Instance methods

Response.arrayBuffer()

Returns a promise that resolves with an ArrayBuffer representation of the response body.

Response.blob()

Returns a promise that resolves with a Blob representation of the response body.

Response.bytes()

Returns a promise that resolves with a Uint8Array representation of the response body.

Response.clone()

Creates a clone of a Response object.

Response.formData()

Returns a promise that resolves with a FormData representation of the response body.

Response.json()

Returns a promise that resolves with the result of parsing the response body text as JSON.

Response.text()

Returns a promise that resolves with a text representation of the response body.

Examples

Fetching an image

In our basic fetch example (run example live) we use a simple fetch() call to grab an image and display it in an <img> element. The fetch() call returns a promise, which resolves to the Response object associated with the resource fetch operation.

You'll notice that since we are requesting an image, we need to run Response.blob to give the response its correct MIME type.

js

const image = document.querySelector(".my-image");fetch("flowers.jpg") .then((response) => response.blob()) .then((blob) => { const objectURL = URL.createObjectURL(blob); image.src = objectURL; });

You can also use the Response() constructor to create your own custom Response object:

js

const response = new Response();

A PHP Call

Here we call a PHP program file that generates a JSON string, displaying the result as a JSON value.

js

// Function to fetch JSON using PHPconst getJSON = async () => { // Generate the Response object const response = await fetch("getJSON.php"); if (response.ok) { // Get JSON value from the response body return response.json(); } throw new Error("*** PHP file not found");};// Call the function and output value or error message to consolegetJSON() .then((result) => console.log(result)) .catch((error) => console.error(error));

Specifications

Specification
Fetch Standard
# response-class

Browser compatibility

BCD tables only load in the browser

See also

Response - Web APIs | MDN (2024)
Top Articles
Wild Rift: How to Get Wild Cores?
Managing Site Factory accounts — Acquia Docs
Www.mytotalrewards/Rtx
Splunk Stats Count By Hour
DPhil Research - List of thesis titles
Skamania Lodge Groupon
Directions To Franklin Mills Mall
Fort Carson Cif Phone Number
Mcoc Immunity Chart July 2022
Emmalangevin Fanhouse Leak
Vanadium Conan Exiles
Weather In Moon Township 10 Days
Remnant Graveyard Elf
Hssn Broadcasts
Tnt Forum Activeboard
Napa Autocare Locator
Prosser Dam Fish Count
A Biomass Pyramid Of An Ecosystem Is Shown.Tertiary ConsumersSecondary ConsumersPrimary ConsumersProducersWhich
Fleet Farm Brainerd Mn Hours
Craigslist Rentals Coquille Oregon
UCLA Study Abroad | International Education Office
Mcclendon's Near Me
Annapolis Md Craigslist
Rgb Bird Flop
Dell 22 FHD-Computermonitor – E2222H | Dell Deutschland
Bridgestone Tire Dealer Near Me
Scat Ladyboy
Mrstryst
Chilangos Hillsborough Nj
Kelley Blue Book Recalls
Merkantilismus – Staatslexikon
Trizzle Aarp
Felix Mallard Lpsg
Legit Ticket Sites - Seatgeek vs Stubhub [Fees, Customer Service, Security]
Noaa Marine Weather Forecast By Zone
Jack In The Box Menu 2022
Man Stuff Idaho
COVID-19/Coronavirus Assistance Programs | FindHelp.org
Doe Infohub
Crystal Glassware Ebay
Spurs Basketball Reference
The Nikki Catsouras death - HERE the incredible photos | Horror Galore
Craigslist Houses For Rent Little River Sc
Syrie Funeral Home Obituary
Myra's Floral Princeton Wv
Wood River, IL Homes for Sale & Real Estate
Free Carnival-themed Google Slides & PowerPoint templates
Ubg98.Github.io Unblocked
Room For Easels And Canvas Crossword Clue
Equinox Great Neck Class Schedule
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6674

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.