如何使用Postman 發送帶 cookie 的請求

如何使用Postman 發送帶 cookie 的請求

有一個接口,請求參數需要使用到Cookie。

測試接口: http://localhost:8080/v1/getUserList

在Postman中,直接發送請求參數,不配置cookie:後端返回結果提示:cookie校驗錯誤。
如下圖
在這裏插入圖片描述
因此需要配置Cookie,配置方式:KEY寫Cookie,VALUE 寫 鍵值對,使用 = 。具體如下圖
在這裏插入圖片描述
配置好以後再次請求接口,接口請求成功,返回正確的結果說明cookie配置成功
在這裏插入圖片描述

接口邏輯如下:

@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 爲空";
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章