简体   繁体   English

在Yesod生态系统中,对某些文本进行urlencode的最佳方法是什么?

[英]In the Yesod ecosystem, what is the best way to urlencode some Text?

I'd like to url-encode some Text (eg, replace each space with a %20, etc.). 我想对一些文本进行url编码(例如,用%20替换每个空格等)。 I found "HTTP" Network.HTTP.Base.urlEncode and could use that, but I'm wondering if there's something else that's normally used in the Yesod ecosystem. 我找到了“HTTP”Network.HTTP.Base.urlEncode并且可以使用它,但我想知道Yesod生态系统中是否还有其他通常使用的东西。

Unfortunately, due to the complexity of URL escaping, the real answer is "it depends." 不幸的是,由于URL转发的复杂性,真正的答案是“它取决于”。 There are slightly different rules for the percent encoding of path segments and query strings, for example. 例如,路径段和查询字符串的百分比编码规则略有不同。

I don't know exactly what you're trying to encode, but I'd recommend sticking to the http-types package. 我不知道你要编码的确切内容,但我建议坚持使用http-types包。 One place to start would be urlEncode , though there are many other functions in that packages (such as encodePathSegments mentioned by @jamshidh) which are worth looking at. 一个开始的地方是urlEncode ,尽管该包中有许多其他功能(例如encodePathSegments提到的encodePathSegments)值得一看。

As I understand it, URI encoding is complicated. 据我了解,URI编码很复杂。 However for my simple case, I was able to get by with the uri-encode package. 但是对于我的简单案例,我能够使用uri-encode包。

All I needed was: 我只需要:

encode :: String -> String

As you can imagine, all it does it take a string, and give you back the URI-encoded version. 你可以想象,所有这一切都需要一个字符串,并返回URI编码版本。

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

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