javascript - Prevent axiosfetch redirection - Stack Overflow

I have a little problem with my app:I have to get some cookies given in the response of one URL, which

I have a little problem with my app: I have to get some cookies given in the response of one URL, which is obtained by making a request to another 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'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(
                    '',
                    {
                        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: '/',
  _bodyInit: '<!DOCTYPE html> <html> the html code</html> ',
  _bodyText: '<!DOCTYPE html> <html> the html code</html> ' }

I have a little problem with my app: I have to get some cookies given in the response of one URL, which is obtained by making a request to another 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'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> ' }
Share Improve this question edited Nov 22, 2023 at 8:57 Shivo'ham 3,1802 gold badges20 silver badges41 bronze badges asked Aug 18, 2017 at 12:36 Alexandre MichaudAlexandre Michaud 2411 gold badge3 silver badges12 bronze badges 2
  • 1 Did you find a solution to this? Having a very similar issue. – Viktor Nilsson Commented Feb 15, 2018 at 9:43
  • Hi, i didn't find any solutions, but i passed around by using Cookies instead of it. :/ Hope it'll help – Alexandre Michaud Commented May 16, 2018 at 20:42
Add a ment  | 

1 Answer 1

Reset to default -2

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744361525a4570493.html

相关推荐

  • javascript - Prevent axiosfetch redirection - Stack Overflow

    I have a little problem with my app:I have to get some cookies given in the response of one URL, which

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信