Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!