簡體   English   中英

防止 axios/fetch 重定向

[英]Prevent axios/fetch redirection

我的應用程序有一個小問題:我必須在一個 url 的響應中獲取一些 cookie,其中一個是通過向另一個 url 發出請求而獲得的。 請求已完成,但我無法獲得 cookie,因為當我獲得請求的結果時,我被重定向到/重定向到該站點。 所以我的問題是:有沒有辦法授權第一個重定向,而不是第二個?

我很清楚我正在使用 react-native,並且 axios 解決方案maxRedirects: 1在我的情況下不起作用。 redirect: 'manual' in fetch。

請求代碼:

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

這是我的代碼,我可以解決,防止獲取重定向。

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;

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM