Handle Redirects

Control redirect behavior and track the redirect chain.

Code

Utilities
// Follow redirects (default)
const response = await fetch('https://httpbin.org/redirect/2');
({
  redirected: response.redirected,
  finalUrl: response.url,
  status: response.status
});
Browser·fetch() may be limited by CORS

Redirect Modes

// Don't follow redirects - get 3xx response
const manual = await fetch('https://httpbin.org/redirect/1', {
  redirect: 'manual'
});
// manual.status === 302

// Error on redirect
const noRedirect = await fetch(url, {
  redirect: 'error'
});
// Throws TypeError on redirect

Test Endpoints

  • /redirect/n - Redirect n times then return 200
  • /redirect-to?url=X - Redirect to specific URL
  • /absolute-redirect/n - Absolute URL redirects
  • /relative-redirect/n - Relative URL redirects

More JavaScript Snippets