Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WebAPI fetch follow redirects?

Does fetch follow HTTP 30x redirects?

like image 270
Ben Aston Avatar asked Oct 19 '17 15:10

Ben Aston


People also ask

Does fetch follow redirects?

Normally, fetch transparently follows HTTP-redirects, like 301, 302 etc.

How does fetch Work API?

The Fetch API allows you to asynchronously request for a resource. Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.

CAN REST API redirect?

In response to a REST API request, the Nest API server returns a redirect. The REST client detects the redirect and requests the page that the client was redirected to. Some HTTP implementations do not forward the Authorization header to the redirected URI, and this results in a 401 Unauthorized error.

Is fetch a callback?

fetch() allows you to make network requests similar to XMLHttpRequest (XHR). The main difference is that the Fetch API uses Promises, which enables a simpler and cleaner API, avoiding callback hell and having to remember the complex API of XMLHttpRequest.


1 Answers

Yes. Check this.

Checking to see if the response comes from a redirected request is as simple as checking this flag on the Response object.

 if (response.redirected) {
   //...
 }

You can disable it:

fetch("awesome-picture.jpg", { redirect: "error" }).then(function(response) {
  //some stuff
}).then(function(imageBlob) {
  //some other stuff
});
like image 51
Héctor Avatar answered Oct 15 '22 20:10

Héctor