• 如何使用Postman 发送带 cookie 的请求


    如何使用Postman 发送带 cookie 的请求

    有一个接口,请求参数需要使用到Cookie。

    测试接口: http://localhost:8080/v1/getUserList

    在Postman中,直接发送请求参数,不配置cookie:后端返回结果提示:cookie校验错误。

    如下图

    因此需要配置Cookie,配置方式:KEY写Cookie,VALUE 写 键值对,使用 = 。具体如下图

    配置好以后再次请求接口,发现请求成功

    接口逻辑如下:

    @RequestMapping(value = "getUserList", method = RequestMethod.POST)
    
    @ApiOperation(value = "获取用户列表", httpMethod = "POST")
    
    public String getUserList(HttpServletRequest request, @RequestBody User user) {
    
        Cookie[] cookies = request.getCookies();
    
        for (Cookie cookie : cookies) {
    
            if (cookie.getName().equals("login1") && cookie.getValue().equals("true")) {
    
                if (user.getName().equals("zhangsan")) {
    
                    User u = new User();
    
                    u.setName("lisi");
    
                    return u.toString();
    
                } else {
    
                    return "名字错误";
    
                }
    
            } else {
    
                return "cookie错误";
    
            }
    
    
        }
    
        return"cookie 为空";
    
    }

  • 相关阅读:
    CF 436D 最小生成树
    HDU 1847 博弈
    ZOJ 3666 博弈 SG函数
    zoj3675 BFS+状态压缩
    HDU 4734 F(x) 数位DP
    HDU 3709 Balanced Number 数位DP
    HDU 3555 数位DP
    HDU 4336 Card Collector
    HDU4340 Capturing a country DP
    CF 351A
  • 原文地址:https://www.cnblogs.com/eathertan/p/12637672.html
Copyright © 2020-2023  润新知