[英]Encoding a string as per RFC 1738 in NodeJs
I want to encode a string as per RFC 1738 in NodeJs app. 我想根据NodeJs应用程序中的RFC 1738对字符串进行编码。 This is required in Twitter API documentation . 这在Twitter API 文档中是必需的。 I found no package to achieve this. 我发现没有包来实现这个目标。 Has someone done this already? 有人这样做了吗? I wanted too register my webhook url to twitter app. 我也希望将我的webhook网址注册到Twitter应用程序。
You can use the native javascript method encodeURIComponent . 您可以使用本机javascript方法encodeURIComponent 。
To be compliant with the RFC you would need to handle these additional characters: 要符合RFC,您需要处理这些附加字符:
function rawurlencode (str) {
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A')
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.