我应该如何将字典编码为HTTP GET查询字符串?

cdl*_*ary 7 http query-string

HTTP GET查询字符串是键/值对的有序序列:

?spam=eggs&spam=ham&foo=bar
Run Code Online (Sandbox Code Playgroud)

是,具有某些语义,相当于以下字典:

{'spam': ['eggs', 'ham'], 'foo': bar}
Run Code Online (Sandbox Code Playgroud)

这恰好适用于所请求页面的布尔属性:

?expand=1&expand=2&highlight=7&highlight=9
{'expand': [1, 2], 'highlight': [7, 9]}
Run Code Online (Sandbox Code Playgroud)

如果要停止扩展id为2的元素,只需将其弹出expand值,然后再次对查询字符串进行urlencode.但是,如果你有一个更多的模态属性(有3个以上的选择),你真的想要代表一个这样的结构:

{'highlight_mode': {7: 'blue', 9: 'yellow'}}
Run Code Online (Sandbox Code Playgroud)

其中相应id键的值是已知枚举的一部分.将此编码为查询字符串的最佳方法是什么?我正在考虑使用一系列两元组,如下所示:

?highlight_mode=(7,blue)&highlight_mode=(9,yellow)
Run Code Online (Sandbox Code Playgroud)

编辑:知道与约定相关联的任何名称也是很好的.我知道可能没有,但很高兴能够使用名称而不是示例来讨论具体的事情.谢谢!

Kon*_*lph 8

通常的方法是这样做:

highlight_mode[7]=blue&highlight_mode[9]=yellow
Run Code Online (Sandbox Code Playgroud)

AFAIR,相当多的服务器端语言实际上支持这种开箱即用,并将为这些值生成一个很好的字典.