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

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM