Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
How to append `?token=...` to the redirect URL when fetch API got 302?

As title. I'm very new to this topic so I will use the example to describe what I want to achieve:

  1. I'm browsing a Git-pages website with some images there. When I clicked those images, I found that the URL is appended with ?token=DSKLFJOS....
  2. Now if I only copy the URL without this token, then I will get 404 as a response.
  3. I found that the 404 is actually the second request. The first request is 302 and the token can be found from the response.
  4. So, what I want to achieve is that I want to use fetch API to try to get the token from 302, and append it to the URL of the (redirect) request ?token=... so that the second request will get 200.

My thought: Maybe this can be achieved after I learn more about the credentials and/or redirect options of the fetch API. But I'm not sure on this!

This is the current code that will fail with 404(on the second request). It is not written by me, so I don't understand it. (and sorry that I cannot post the full code since it's a private content)

    fetch(filePath)
      .then(response => response.arrayBuffer())
      .then(arrayBuffer => callback(arrayBuffer, arrayBuffer.byteLength));

Thanks for your reading. If I any of my idea is incorrect please feel free to correct me and post it as an answer!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Maybe something like this will work. Basically, you just check the status code, and if it's 302 you will re-fetch the response with the token, then if the status is still greater than 300 you return null, if not you return the JSON response.

async function getImage(url, token) {
  let response = await fetch(url);
  
  // You can also say if (response.status >= 300 && response.status < 400)
  if (response.status === 302) {
    response = await fetch(`${url}?token=${token}`);
  }
  
  if (response.status >= 300) {
    return null;
  }
  
  return response.json();
}

Whit the headers location would be something like this:

async function getImage(url) {
  let response = await fetch(url);
  
  if (response.status === 302) {
    response = await fetch(`${url}?token=${response.headers.get('location')}`);
  }
  
  return response.json();
}

I remove the second if just to simplify the answer and just having in mind the request would be successful.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can make another request with the new URL with fetch(originalResponse.url + token).

The url read-only property of the Response interface contains the URL of the response. The value of the url property will be the final URL obtained after any redirects.

from https://developer.mozilla.org/en-US/docs/Web/API/Response/url

Here's an example:

let token = "abcdef"
fetch("page-that-redirects.com/")
  .then(response => {
    if (response.status === 404) {
      fetch(response.url + "?token=" + token)
        .then(/* do something */)
    }
  })

// Or with async/await:
let response = await fetch("page-that-redirects.com/")
if (response.status === 404) {
  let newResponse = await fetch(response.url + "?token=" + token)
  /* do something */
}


about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda