V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chaleaochexist
V2EX  ›  程序员

请教 url encode 问题

  •  
  •   chaleaochexist · 100 天前 · 1210 次点击
    这是一个创建于 100 天前的主题,其中的信息可能已经有所发展或是发生改变。
    https://192.168.1.1:25505/api/v1/query?query=sum(a:b:c:d:e{a="", b="c"}) by (namespace)
    

    用 postman 转成 curl 是这样子的

    curl --location --globoff 'https://192.168.1.1:25505/api/v1/query?query=sum(a%3Ab%3Ac%3Ad%3Ae{a%3D%22%22%2C%20b%3D%22c%22})%20by%20(namespace)'
    

    可以看到对参数部分编码了:

    1. 冒号 :
    2. 等于号 =
    3. 引号 "
    4. 逗号 ,

    就是没有 encode 括号()和大括号{}

    我的问题是: 为啥没有 encode 大小括号?

    11 条回复    2024-01-18 14:07:27 +08:00
    lzgshsj
        1
    lzgshsj  
       100 天前
    有什么问题,你自己在控制台 encodeURI 一下就知道了。括号本来就不需要 encode 。
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
    MegatronKing
        2
    MegatronKing  
       100 天前
    这是个好问题,我也遇到过。比如 * 号,RFC 规范里面没有明确定义是否需要 encode 导致不同版本的实现不相同,有的库会 encode 有的不会,很容易因为前后端不一致出现问题。
    Subfire
        3
    Subfire  
       100 天前
    为什么要 encode? 现代浏览器早已支持了 URL 中有 Unicode
    5200
        4
    5200  
       100 天前
    要不你直接 base64 一下,https://192.168.1.1:25505/api/v1/query?query=base64 码,
    get 方式记得挺多问题的,复杂编码最好用 post 或者 head 头试试。
    chaleaochexist
        5
    chaleaochexist  
    OP
       100 天前
    @lzgshsj 你这是个函数和标准有啥关系? mozilla 也不是标准.

    @MegatronKing 那就是说, 最后还是要看客户端和服务端之间的协议了...

    @Subfire 因为我不用浏览器.

    @5200 额...base64 也支持吗? 那还真不错. 我学习一下.
    morenacl
        6
    morenacl  
       100 天前
    sum%28a%3Ab%3Ac%3Ad%3Ae%7Ba%3D%22%22%2C%20b%3D%22c%22%7D%29%20by%20%28namespace%29%0A, 这是标准 url 编码后的结果
    lzgshsj
        7
    lzgshsj  
       100 天前
    @chaleaochexist 额。。。首先 postman 是 electron 的,electron 是 nodejs ,你说我为啥要给你看 js 的文档。其次,你压根就没看我给的链接吗。里面都说了为什么 encode 会保留括号。

    encodeURI() escapes all characters except:

    A–Z a–z 0–9 - _ . ! ~ * ' ( )

    ; / ? : @ & = + $ , #
    The characters on the second line are characters that may be part of the URI syntax, and are only escaped by encodeURIComponent(). Both encodeURI() and encodeURIComponent() do not encode the characters -.!~*'(), known as "unreserved marks", which do not have a reserved purpose but are allowed in a URI "as is". (See RFC2396)
    virusdefender
        8
    virusdefender  
       100 天前
    这个大家的实现其实差异挺大的,如果不放心可以把所有的字符都编码,也是可以的。
    jiangzm
        9
    jiangzm  
       100 天前
    #4 base64 也不能直接用做 url param ,需要转义几个字符,这种特殊的 base64 有另外的名字叫 safeBase64
    lzgshsj
        10
    lzgshsj  
       100 天前
    另外,根据 https://tc39.es/ecma262/multipage/global-object.html#sec-uri-handling-functions
    其中
    > NOTE 1
    The set of reserved characters is based upon RFC 2396 and does not reflect changes introduced by the more recent RFC 3986.
    可以得知是根据 RFC 2396 的保留字符来的。
    然后,根据 RFC 2396 https://www.ietf.org/rfc/rfc2396.txt

    > 2.2. Reserved Characters
    reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
    "$" | ","
    > 2.3. Unreserved Characters
    unreserved = alphanum | mark

    mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"

    这下应该清楚了吧
    KouShuiYu
        11
    KouShuiYu  
       100 天前
    ! # $ & ' ( ) * + , / : ; = ? @ [ ]
    保留字符不需要编码
    https://en.wikipedia.org/wiki/Percent-encoding
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2666 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:31 · PVG 23:31 · LAX 08:31 · JFK 11:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.