zoukankan      html  css  js  c++  java
  • 如何使用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 为空";
    
    }

  • 相关阅读:
    关于数据集的划分--训练集、验证集和测试集
    关于过拟合的问题总结
    paddle 09-13
    关于NLP多分类任务评价指标的总结
    数组题解
    多进程-协程
    多任务-进程
    多任务-线程
    网络-tcp
    网络-udp
  • 原文地址:https://www.cnblogs.com/eathertan/p/12637672.html
Copyright © 2011-2022 走看看