防止axios/fetch重定向

15
我在我的应用程序中遇到了一个小问题: 我需要从一个URL的响应中获取一些cookie,这个URL是通过向另一个URL发出请求获得的。请求已经发送了,但是我无法获取到cookie,因为当我获取到请求结果时,我被重定向到了该网站。 所以我的问题是:有没有办法授权第一次重定向,但不授权第二次重定向?
我正在使用react-native,Axios解决方案中的maxRedirects: 1在我的情况下不起作用。对于fetch中的redirect: 'manual'也是一样。
请求代码:
fetch(
                    'https://gitlab.blablabla.fr/users/auth/ldapmain/callback',
                    {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                            'accept-charset':'utf-8',
                        },
                        body: querystring.stringify({
                            authenticity_token: this.state.token,
                            username: this.state.username,
                            password: this.state.password,
                        }),
                        redirect: 'manual',
                        credentials: 'include',
                        withCredentials: 'true',

                    }
                ).then( (responseData2) => {
                        console.log(this.state.username +"/"+ this.state.password+ "/"+this.state.token + "/"+ this.state.utf8);
                        console.log(responseData2);


                    })

并显示出的响应:

Response {
  type: 'default',
  status: 200,
  ok: true,
  statusText: undefined,
  headers: 
   Headers {
     map: 
      { 'x-version-id': [Object],
        server: [Object],
        'cache-control': [Object],
        date: [Object],
        'content-type': [Object],
        'content-length': [Object],
        'content-security-policy': [Object],
        'accept-ranges': [Object],
        'x-request-id': [Object],
        'last-modified': [Object],
        'x-frame-options': [Object] } },
  url: 'https://mattermost.blablabla.fr/',
  _bodyInit: '<!DOCTYPE html> <html> the html code</html> ',
  _bodyText: '<!DOCTYPE html> <html> the html code</html> ' }

1
你找到解决办法了吗?我有一个非常类似的问题。 - undefined
嗨,我没有找到任何解决方案,但是我通过使用Cookies来绕过了它。:/ 希望这能有所帮助。 - undefined
2个回答

0
一个可能的解决方案是通过手动检查响应状态和头部来处理重定向。您可以修改代码以手动跟踪重定向并从头部中获取Cookie。
fetch('https://gitlab.blablabla.fr/users/auth/ldapmain/callback', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'accept-charset': 'utf-8',
  },
  body: querystring.stringify({
    authenticity_token: this.state.token,
    username: this.state.username,
    password: this.state.password,
  }),
  credentials: 'include',
})
  .then((response) => {
    // Check if the response is a redirect
    if (response.redirected) {
      // Manually follow the redirection
      return fetch(response.url, {
        method: 'GET', // or 'POST' if needed
        credentials: 'include',
      });
    } else {
      return response;
    }
  })
  .then((responseData) => {
    // Now you can access the cookies from the headers
    const cookies = responseData.headers.get('set-cookie');
    console.log('Cookies:', cookies);

    // Continue handling the response as needed
    console.log(responseData);
  })
  .catch((error) => {
    console.error('Error:', error);
  });

-2

这是我的代码,我可以解决并阻止重定向。

import 'whatwg-fetch';
fetch('/api/user/get_user_profile', 
    { 
        method: 'GET',
        headers: { 'Content-Type': 'application/json' },
        data: JSON.stringify({
            sHash: sHash
        }),
        redirect: 'manual',
    }
)

这是后端代码

res.status(200);
res.send(content);
return;

输入图像描述


5
HTTP状态码 200 不属于“重定向”类型,而 3** 系列的状态码属于重定向类型。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接