首页 > 解决方案 > 从 ios 和 android 中的 react-native 应用程序获取响应中的不同 Set-Cookie 标头

问题描述

这很奇怪,但我只在 Android 设备上的 react-native 应用程序中看不到我的 express 服务器中的一些 set-cookie 标头!

我在我的 node.js 服务器上使用 express

const express = require('express');
const csrf = require('csurf');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const bodyParser = require('body-parser');

app.use(cookieParser());
app.use(session({
    name: 'sid',
    secret: CONSTANTS.SESSION_SECRET,
    resave: false,
    saveUninitialized: true
}));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());

const csrfProtection = csrf();
app.use(csrfProtection);

app.get('/', (request, response, next) => {
    response.cookie('csrftoken', request.csrfToken());
    response.status(200).send(...);
});

我只是设置 csrftoken cookie 并发送一些响应;

在客户端上,我首先获得邮件页面:

fetch(URL_INDEX, {
    method: 'GET',
    headers: {
        'User-Agent': USER_AGENT,
        Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    }
}).then((response) => {
    //check response here <--------------------------------
    processCookies(response);
    return response;
})

我在调试器中看到的 fetch 答案:

在IOS中

Headers
  map:Object
    connection:"keep-alive"
    content-length:"11716"
    content-type:"text/html; charset=utf-8"
    date:"Fri, 08 Feb 2019 10:58:25 GMT"
    etag:"W/"2dc4-Ew7HRSMcOHECQlBtXIfAuuPw/Vs""
    set-cookie:"csrftoken=bzKoDm5w-CdiLpGk0NWZ1TxaACRNJoRvVeng; Path=/, sid=s%3AGLvycaobuCL7xyIiNRBCXHSZU3SmHmAJ.vv9dzPurvicFvMb%2FkeB9Lshy6ZAQ0SvsMm8cR2ij9O8; Path=/; HttpOnly"
    x-powered-by:"Express"

在安卓中

Headers
  map: Object
    connection:"keep-alive"
    content-length:"11716"
    content-type:"text/html; charset=utf-8"
    date:"Fri, 08 Feb 2019 10:52:52 GMT"
    etag:"W/"2dc4-Ew7HRSMcOHECQlBtXIfAuuPw/Vs""
    set-cookie:"sid=s%3AOgeCB1Xsu8yA0SKiXF5Xgdc5pE-gSjfs.cDNHgzc41X1JmnUKmY6Hn3J2%2BrjxbXpBt7%2FhXwN6HbQ; Path=/; HttpOnly"
    x-powered-by:"Express"

如您所见,csrftoken cookie 存在于 ios 上的响应中,但我们在 android 上看不到它。

有任何想法吗?

标签: expressreact-nativefetch

解决方案


推荐阅读