如何使用从帖子请求发送cookie auth的API服务,然后将其与Get请求一起使用?
我和Fetch一起玩了一点,然后我陷入了问题。
巴西这里有一项API服务,可以响应公共巴士服务流量。为了获得首先需要的资源,发送邮政请求,该请求为您提供cookie auth,然后您可以提出获取请求。
在此阶段,我可以获得邮政cookie,但是稍后尝试获取数据时,它会返回401错误和同一站点。我想这是因为我无法将cookie发送到Get请求中,对吗? 这是控制台的错误:
xhrgethttp://api.olhovivo.sptrans.com.br/v2.1/posicao/linha?codigolinha = 856
[http/1.1 401未经授权的13ms]
一些cookie滥用了推荐的“ samesite”属性2
cookie“ apicredentials”被拒绝,因为它在跨站点的上下文中,其“ samesite”为“ lax”或“严格”。 Autenticar
cookie“ apicredentials”被拒绝,因为它在跨站点的上下文中,其“ samesite”为“ lax”或“严格”。 Autenticar
bust.html:32:28
在Python中,我可以轻松地使用会话命令来完成此操作,但我无法在JavaScript中获取。
有人知道如何管理该代码以工作并从API服务中获取响应吗?
//这是我的代码:
<!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>
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 the POST auth cookie but when trying to GET data later it returns an 401 error and same site. I guess it is because I am not being able to send the cookie into the 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
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.
// 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论