'Prevent axios/fetch redirection

I have a little problem in my app: I have to get some cookies given in the response of one url, wich one is obtained by making a request to an other url. The request is done, but I'm not able to get the cookies, because when I get the result of my request, I get redirected to/of the site. So my question is: is there a way to authorize the first redirection, but not the second?

I precize that I'm using react-native, and the axios solution maxRedirects: 1 is not working in my case. Same for the redirect: 'manual' in fetch.

The request code:

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);


                    })

And the response that is shown :

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> ' }


Solution 1:[1]

This is my code, I can solve, prevent the fetch redirectiong.

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

This is backend code

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

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1