a Æ*Na¡ ã@sDdZddlmZddlmZdZdd„Zdd„Zdd „Zd d „Zd S) z8Implementation of nested form-data encoding function(s).é)Ú basestring)Ú urlencodecsftttf‰t|ƒ}tdd„|Dƒƒs,tdƒ‚|}t‡fdd„|DƒƒrPt|ƒ}q0t|g|¢Ri|¤ŽS)aTHandle nested form-data queries and serialize them appropriately. There are times when a website expects a nested form data query to be sent but, the standard library's urlencode function does not appropriately handle the nested structures. In that case, you need this function which will flatten the structure first and then properly encode it for you. When using this to send data in the body of a request, make sure you specify the appropriate Content-Type header for the request. .. code-block:: python import requests from requests_toolbelt.utils import formdata query = { 'my_dict': { 'foo': 'bar', 'biz': 'baz", }, 'a': 'b', } resp = requests.get(url, params=formdata.urlencode(query)) # or resp = requests.post( url, data=formdata.urlencode(query), headers={ 'Content-Type': 'application/x-www-form-urlencoded' }, ) Similarly, you can specify a list of nested tuples, e.g., .. code-block:: python import requests from requests_toolbelt.utils import formdata query = [ ('my_list', [ ('foo', 'bar'), ('biz', 'baz'), ]), ('a', 'b'), ] resp = requests.get(url, params=formdata.urlencode(query)) # or resp = requests.post( url, data=formdata.urlencode(query), headers={ 'Content-Type': 'application/x-www-form-urlencoded' }, ) For additional parameter and return information, see the official `urlencode`_ documentation. .. _urlencode: https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode css|]}t|ƒVqdS©N)Ú _is_two_tuple)Ú.0Úi©rú{/private/var/folders/js/6pj4vh5d4zd0k6bxv74qrbhr0000gr/T/pip-target-22xwyzbs/lib/python/requests_toolbelt/utils/formdata.pyÚ Nózurlencode..zQExpected query to be able to be converted to a list comprised of length 2 tuples.c3s|]\}}t|ˆƒVqdSr)Ú isinstance)rÚ_Úv©Zexpand_classesrr r Sr ) ÚdictÚlistÚtupleÚ _to_kv_listÚallÚ ValueErrorÚanyÚ_expand_query_valuesÚ _urlencode)ÚqueryÚargsÚkwargsÚoriginal_query_listÚ query_listrrr r sA  rcCst|dƒrt| ¡ƒS|S)NÚitems)Úhasattrrr)Z dict_or_listrrr rYs  rcCst|ttfƒot|ƒdkS)Nr)r rrÚlen)Úitemrrr r_srcsXg}|D]J\}}t|tƒr*| ||f¡q|d‰t|ƒ}| ‡fdd„|Dƒ¡q|S)Nz[%s]c3s|]\}}ˆ||fVqdSrr)rÚkr©Zkey_fmtrr r kr z'_expand_query_values..)r rÚappendrÚextend)rrÚkeyÚvalueZ value_listrr#r rcs  rN) Ú__doc__Ú_compatrrrÚ__all__rrrrrrr Ús  O