防止axios /提取重定向

Ale*_*aud 5 javascript fetch react-native axios

我的应用程序中有一个小问题:我必须在一个URL的响应中获得一些cookie,其中一个是通过对另一个URL的请求获得的。请求已完成,但是我无法获取cookie,因为当我获得请求的结果时,我将被重定向到该站点。 所以我的问题是:是否有一种方法可以授权第一个重定向,但不能授权第二个重定向?

我以为我使用的是react-native,而axios解决方案maxRedirects: 1在我的情况下不起作用。同为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);


                    })
Run Code Online (Sandbox Code Playgroud)

并显示响应:

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> ' }
Run Code Online (Sandbox Code Playgroud)

ava*_*dev -2

这是我的代码,我可以解决,防止获取重定向。

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

这是后端代码

res.status(200);
res.send(content);
return;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • HTTP 状态``200```` 不是“重定向”类型的一部分,````3**```` 是。 (5认同)