[Python] Encode URL with nested dictionaries

Discussão em 'Python' iniciado por Stack, Janeiro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am using an API that doesn't accept JSON in the requests' bodies. It uses x-www-form-urlencoded.

    Thus, if I have to send this dict:

    {
    'a': 1,
    'b': 2,
    'c': {
    'k': 3,
    'v': 4,
    'l': {
    'p': 5,
    'q': 6,
    },
    },
    }


    It have to be encoded like this:

    a=1
    b=2
    c[k]=3
    c[v]=4
    c[l][p]=5
    c[l][q]=6


    However, urllib.parse.urlencode won't parse the dict this way. Instead it's going to encode c content literally and put within it (c={encodeddict}).

    I tried to implement some encoder like this by myself, but I couldn't get to deal with multiple nested dicts. I only managed to encode 1-level dicts (like c[k]=3), but not recursively to the last level (c[l][p]=5, for example).

    What is the best way to achieve this kind of encoding in Python 3?

    Continue reading...

Compartilhe esta Página