Esta tradução está incompleta. Por favor, ajude a traduzir este artigo.

A interface Response da Fetch API representa a resposta para uma requisição.

Você pode criar um novo objeto Response usando o construtor Response.Response(), porém é mais provável você encontrar um objeto Response sendo retornado como o resultado de uma outra operação da API, por exemplo um service worker Fetchevent.respondWith, ou um simples GlobalFetch.fetch().

Construtor

Response()
Cria um novo objeto Response.

Propriedades

Response.headers Somente leitura
Contém o objeto Headers associado à resposta.
Response.ok Somente leitura
Contém um valor booleano indicando se a resposta foi bem sucedida ("status" no intervalo 200-299) ou não.
Response.redirected Somente leitura
Indica se a resposta é ou não o resultado de um redirecionamento; isto é, sua lista de URL tem mais de uma entrada.
Response.status Somente leitura
Contém o código de "status" da resposta (ex., 200 para um sucesso).
Response.statusText Somente leitura
Contém a mensagem de "status" correspondente ao código de "status" (ex., OK para 200).
Response.type Somente leitura
Contém o tipo da resposta (ex., basic, cors).
Response.url Somente leitura
Contém a URL de resposta.
Response.useFinalURL
Contém um valor boleano indicando se essa é a URL final da resposta.

Response implementa Body, por isso também tem as seguintes propriedades disponíveis:

Body.body Somente leitura
Um simples "getter" para ler do conteúdo do corpo através da interface ReadableStream.
Body.bodyUsed Somente leitura
Armazena um Boolean que indica se o corpo já foi utilizado em uma resposta.

Métodos

Response.clone()
Cria uma cópia do objeto Response.
Response.error()
Retorna um novo objeto Response associado a um erro de rede.
Response.redirect()
Cria uma nova resposta com uma URL diferente.

Response implementa Body, por isso também tem as seguintes propriedades disponíveis:

Body.arrayBuffer()
Recebe um "stream" Response e lê até a conclusão. Retorna uma "promise" que resolve com um  ArrayBuffer.
Body.blob()
Recebe um "stream" Response e lê até a conclusão. Retorna uma "promise" que resolve com um Blob.
Body.formData()
Recebe um "stream" Response e lê até a conclusão. Retorna uma "promise" que resolve com um objeto FormData.
Body.json()
Recebe um "stream" Response e lê até a conclusão. Retorna uma "promise" que resolve com o resultado do parseamento do texto do corpo como JSON.
Body.text()
Recebe um "stream" Response e lê até a conclusão. Retorna uma "promise" que resolve com um USVString (texto).

Exemplos

Em nosso exemplo básico fetch (executar exemplo live) nós usamos uma simples chamada fetch() para pegar uma imagem e exibi-la em uma tag <img>. A chamada fetch() retorna uma "promise", que resolve com o objeto Response associado com o recurso da operação "fetch". Você irá notar que como estamos solicitando uma imagem, nós precisamos executar Body.blob (Response implementa o "body") para dar à resposta seu tipo MIME correto.

var myImage = document.querySelector('.my-image');
fetch('flowers.jpg').then(function(response) {
  return response.blob();
}).then(function(blob) {
  var objectURL = URL.createObjectURL(blob);
  myImage.src = objectURL;
});

Você também pode usar o construtor Response.Response() para criar seu objeto Response personalizado:

var myResponse = new Response();

Especificações

Especificação Status Comentário
Fetch
The definition of 'Response' in that specification.
Padrão em tempo real Initial definition

Compatibilidade entre Navegadores

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic supportChrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39IE No support NoOpera Full support 29Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android Full support YesOpera Android Full support 29Safari iOS Full support 10.1Samsung Internet Android Full support 4.0
Response() constructor
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 15Firefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari Full support 10.1WebView Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference.
Chrome Android Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile Full support YesFirefox Android Full support YesOpera Android Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari iOS Full support 10.3Samsung Internet Android Full support 4.0
headers
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
ok
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
redirected
Experimental
Chrome Full support 60Edge Full support 16Firefox Full support 49IE No support NoOpera Full support 47Safari No support NoWebView Android Full support 60Chrome Android Full support 60Edge Mobile Full support YesFirefox Android Full support 49Opera Android Full support 47Safari iOS No support NoSamsung Internet Android No support No
status
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
statusText
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
type
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
url
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
useFinalURL
ExperimentalDeprecated
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
clone
Experimental
Chrome Full support 42
Full support 42
Full support 41
Disabled
Disabled From version 41: this feature is behind the Experimental Web Platform Features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support YesFirefox Full support 39
Full support 39
Full support 34
Disabled
Disabled From version 34: this feature is behind the dom.fetch.enabled preference. To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 29
Full support 29
Full support 28
Disabled
Disabled From version 28: this feature is behind the Experimental Web Platform Features preference.
Safari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile Full support YesFirefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
error
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE No support NoOpera Full support YesSafari Full support YesWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
redirect
Experimental
Chrome Full support YesEdge Full support YesFirefox Full support YesIE No support NoOpera Full support YesSafari Full support YesWebView Android No support NoChrome Android No support NoEdge Mobile ? Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
trailerChrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
User must explicitly enable this feature.
User must explicitly enable this feature.

Veja também

Etiquetas do documento e colaboradores

Colaboradores desta página: jeovazero
Última atualização por: jeovazero,