如何使用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 为空";

}

猜你喜欢

转载自www.cnblogs.com/eathertan/p/12637672.html