【问题标题】:React Native get only one cookie despite of sending multiple cookies from Spring-Boot尽管从 Spring-Boot 发送了多个 cookie,但 React Native 只得到一个 cookie
【发布时间】:2019-01-11 10:51:52
【问题描述】:

我无法获取 cookie

春天

Spring boot 正在发送多个 cookie,通过使用下面


    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;
    @RestController
    public class SampleController {
        @PostMapping("/test")
        public Object test(HttpSession session, HttpServletResponse response) {
            String cookie1 = "first";
            String cookie2 = "second";
            Cookie firstCookie = new Cookie("uid", cookie1);
            firstCookie.setPath("/");
            firstCookie.setMaxAge(60 * 60 * 24 * 30);
            Cookie secondCookie = new Cookie("token", cookie2);
            secondCookie.setPath("/");
            secondCookie.setMaxAge(60 * 60 * 24 * 30);
            response.addCookie(firstCookie);
            response.addCookie(secondCookie);
        }
    }

反应原生

使用两个 cookie 发送响应, 并在下面反应本机代码示例以捕获 cookie

    function test() {
        fetch(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                credentials: 'include',
                'Content-Type': 'application/json',
            }
        }).then((response) => {
            for (const [name, value] of response.headers) {
                if (name === "set-cookie") {
                    console.log(value)
                }
            }
        });
    }

我使用 response.header 检查了 cookie,但我最后添加了一个(第二个 - cookie2)

React debug result

我也试过 python,邮递员,我可以看到三个 cookie 包括 jsession Postman Cookie View

如何获得两个 cookie?

【问题讨论】:

    标签: spring-boot react-native cookies


    【解决方案1】:

    尝试使用 axios 获取请求

    import axios from 'axios';
    
    // ...
    
    let res = await axios.get(url)
    console.log( res.headers["set-cookie"]) 
    
    // or 
    
    axios.get(url)
      .then(function (response) {
        console.log(response.headers["set-cookie"]);
      });
    

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2017-05-02
      • 1970-01-01
      • 2015-10-28
      • 2018-08-29
      • 2020-08-14
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多