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>