简体   繁体   English

如何使用Scotty / wai设置cookie?

[英]How do I set a cookie with Scotty / wai?

I've browsed the Scotty documentation but I only see the ability to set an HTTP header which seem a bit low level. 我浏览了Scotty文档,但只看到了设置HTTP标头的功能,该标头似乎有点低。

Is there a more elegant way to achieve this? 有没有更优雅的方法来实现这一目标? Third party libraries? 第三方图书馆?

From what I can find, a solution would be to use mapHeader from Network.Wai.Util . 据我mapHeader ,一个解决方案是使用Network.Wai.Util mapHeader But like mentioned this seems quite low level. 但是就像提到的那样,这似乎是一个很低的水平。

There is also the cookie package but it appears to return a fully rendered cookie 'string' only rather then being able to retrieve the key/value. 也有cookie程序包,但它似乎仅返回完全渲染的cookie“字符串”,而不是能够检索键/值。

There is also: https://hackage.haskell.org/package/HTTP-4000.3.9/docs/Network-HTTP-Cookie.html but there seems to be no Expiry parameter? 还有: https://hackage.haskell.org/package/HTTP-4000.3.9/docs/Network-HTTP-Cookie.html : https://hackage.haskell.org/package/HTTP-4000.3.9/docs/Network-HTTP-Cookie.html但是似乎没有Expiry参数吗?

https://hackage.haskell.org/package/scotty-cookie-0.1.0.3/docs/Web-Scotty-Cookie.html https://hackage.haskell.org/package/cookie-0.4.3/docs/Web-Cookie.html https://hackage.haskell.org/package/scotty-cookie-0.1.0.3/docs/Web-Scotty-Cookie.html https://hackage.haskell.org/package/cookie-0.4.3/docs/Web -Cookie.html

import Web.Cookie
import Web.Scotty.Cookie

setCookie $ defaultSetCookie { setCookieName = "example", setCookieValue = "cookieValue" }

The function is actually surprisingly simple: 该函数实际上非常简单:

setCookie :: (Monad m, ScottyError e)
          => SetCookie
          -> ActionT e m ()
setCookie c = addHeader "Set-Cookie" (TL.decodeUtf8 . toLazyByteString $ renderSetCookie c)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM