我正在使用 firebase 动态链接缩短器 api 创建动态链接,在请求中我传递长链接,其中包含我想在我的应用程序中呈现的消息的查询参数。但是当我打开短链接(我得到了回应)时,长链接中传递的所有空格都被转换为 +,我试图从 firebase 仪表板创建类似的链接,但遇到了同样的问题。
我使用了以下文档中给出的 api https://firebase.google.com/docs/reference/dynamic-links/link-shortener
这种行为是意料之中的,只要任何+
符号都跟在查询分隔符之后,浏览器就应该原生支持?
。
根据RFC3986规范、其过时的形式RF2396、RF1738以及多年来的许多链接变化,空格字符 ( 0x20
) 必须在 URI 中编码。
为了与所有方案兼容,它必须被百分比编码为%20
.
然而,URI与的方案查询组件http:
或https:
具有Content-Type
的application/x-www-form-urlencoded
。此内容类型指定空格将被编码为+
,保留字符将根据 URI 规范进行转义,并且非字母数字字符将被百分比编码为%HH
。
所以本质上,这意味着在 URI 中使用or方案+
将其转换回空格。?
http
https
例子:
// URI of a StackOverflow search for "RFC 3986"
"https://stackoverflow.com/search?q=RFC+3986" // ✔ (default used by StackOverflow)
"https://stackoverflow.com/search?q=RFC%203986" // ✔
"https://stackoverflow.com/search?q=RFC 3986" // ✖ (prohibited character)
// URI of "C:\Program Files (x86)\Notepad++\notepad++.exe"
"file:///C:/Program%20Files%20(x86)/Notepad++\notepad++.exe" // ✔
"file:///C:/Program%20Files%20(x86)/Notepad%2B%2B\notepad%2B%2B.exe" // ✔ (verbose, but valid)
"file:///C:/Program Files (x86)/Notepad++\notepad++.exe" // ✖ (spaces must be encoded)
"file:///C:/Program+Files+(x86)/Notepad++\notepad++.exe" // ✖ (not found, `+` not decoded to space)
保留字符的目的是提供一组可与 URI 中的其他数据区分开来的定界字符。在用相应的百分比编码的八位字节替换保留字符方面不同的 URI 是不等价的。对保留字符进行百分比编码,或对与保留字符对应的百分比编码八位字节进行解码,将改变大多数应用程序对 URI 的解释方式。因此,保留集中的字符不受规范化保护,因此可以安全地由特定于方案和特定于生产者的算法用于在 URI 中分隔数据子组件。
由于上述声明,重要的是要注意许多decodeURI
/decodeURIComponent
函数不会将 a 解码+
为空格。由您的代码添加对此的支持,例如Google Closure Library如何在解码之前将它们交换为空格:
/**
* URL-decodes the string. We need to specially handle '+'s because
* the javascript library doesn't convert them to spaces.
* @param {string} str The string to url decode.
* @return {string} The decoded {@code str}.
*/
goog.string.urlDecode = function(str) {
return decodeURIComponent(str.replace(/\+/g, ' '));
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句