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

133
Visualizações
How to use an API service that sends a cookie auth from a POST request to then you use it with GET requests?

I was playing a bit with fetch then i got stuck into an issue.

There is an API service here in Brazil that responses public bus services traffic. To get the resources you first need to send a POST request that gives you a cookie auth and after that you can make the GET requests.

In this stage i can get de POST auth cookie but when trying to GET data later it returns an 401 error and samesite. I guess it is becouse i am not being able to send the cookie into de GET request, am i right? Here is the error from console:

XHRGEThttp://api.olhovivo.sptrans.com.br/v2.1/Posicao/Linha?codigoLinha=856
[HTTP/1.1 401 Unauthorized 13ms]
Some cookies are misusing the recommended “SameSite“ attribute 2
Cookie “apiCredentials” has been rejected because it is in a cross-site context and its “SameSite” is “Lax” or “Strict”. Autenticar
Cookie “apiCredentials” has been rejected because it is in a cross-site context and its “SameSite” is “Lax” or “Strict”. Autenticar
<empty string> bust.html:32:28

In python i can do it easily with the session command but i am not being able to do with fetch in javaScript.

Does anyone knows how to manage that code to work and get the response from the API service.

Thank you very much!!!

// this is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
  <script>

const api_url = 'http://api.olhovivo.sptrans.com.br/v2.1/Posicao/Linha?codigoLinha=856'

const auth_url = 'http://api.olhovivo.sptrans.com.br/v2.1/Login/Autenticar?token=1ce7717aac41a2c9315fb17054b93713459dae903e6914999dcea325dc7baa61'

fetch(auth_url, {
            method: 'POST',
            mode: 'no-cors',
            credentials: 'include',
        })
            .then(response => {
                console.log(response.headers.get('set-cookie')); // undefined
                console.log(document.cookie); // nope
        });



fetch(api_url, {
            mode: 'no-cors',
            credentials: 'include',
        })
            .then(response => response.text())
            .then(result => console.log(result))
            .catch(error => console.log('error', error));


  </script>
    
</body>
</html>
about 4 years ago · Juan Pablo Isaza
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