How to encode dictionaries into HTTP GET request strings?

The HTTP GET request string is an ordered sequence of key / value pairs:

?spam=eggs&spam=ham&foo=bar

It is, with certain semantics, equivalent to the following dictionary:

{'spam': ['eggs', 'ham'], 'foo': bar}

This works well for the logical properties of the requested page:

?expand=1&expand=2&highlight=7&highlight=9
{'expand': [1, 2], 'highlight': [7, 9]}

If you want to stop expanding the element with id 2, just pull it out of the value expandand enter the query string again. However, if you have a more modal property (with 3+ options), you really want to represent the structure as follows:

{'highlight_mode': {7: 'blue', 9: 'yellow'}}

If the values ​​of the corresponding identification keys are part of a known enumeration. What is the best way to encode this into a query string? I am thinking of using a sequence of two tuples, for example:

?highlight_mode=(7,blue)&highlight_mode=(9,yellow)

: , . , , - , . !

+7
4

:

highlight_mode[7]=blue&highlight_mode[9]=yellow

AFAIR, .

+8

, JSON , BASE64 ( - ), .

.

, POST, JSON - .

+4

- , .

{'foo': [1], 'bar': [2, 3], 'fred': 4}

:

?foo[]=1&bar[]=2&bar[]=3&fred=4

, foo (, ) fred ( ).

:

?highlight_mode[7]=blue&highlight_mode[9]=yellow

, .

, , Rails , Rails.

, , .

[], , . , , . , , . Rails []. querystring, , [1], 4.

+3

Python Backend-

import json, base64
param={
        "key1":"val1",
        "key2":[
                {"lk1":"https://www.foore.in", "lk2":"https://www.foore.in/?q=1"},
                {"lk1":"https://www.foore.in", "lk2":"https://www.foore.in/?q=1"}
                ]
        }
encoded_param=base64.urlsafe_b64encode(json.dumps(param).encode())
encoded_param_ready=str(encoded_param)[2:-1]

#eyJrZXkxIjogInZhbDEiLCAia2V5MiI6IFt7ImxrMSI6ICJodHRwczovL3d3dy5mb29yZS5pbiIsICJsazIiOiAiaHR0cHM6Ly93d3cuZm9vcmUuaW4vP3E9MSJ9LCB7ImxrMSI6ICJodHRwczovL3d3dy5mb29yZS5pbiIsICJsazIiOiAiaHR0cHM6Ly93d3cuZm9vcmUuaW4vP3E9MSJ9XX0=
#In JS
var decoded_params = atob(decodeURI(encoded_param_ready));

0

All Articles