首页 > 其他 > 详细

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

时间:2020-04-05 16:38:52      阅读:68      评论:0      收藏:0      [点我收藏+]

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

}

 

技术分享图片

 

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

原文:https://www.cnblogs.com/eathertan/p/12637672.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!