Skip to content

Instantly share code, notes, and snippets.

@BedrosovaYulia
Created July 25, 2019 08:44
Show Gist options
  • Save BedrosovaYulia/2373bf43b4f89ee4479cc04ab1d0ceff to your computer and use it in GitHub Desktop.
Save BedrosovaYulia/2373bf43b4f89ee4479cc04ab1d0ceff to your computer and use it in GitHub Desktop.
def http_build_query(params, topkey = ''):
from urllib.parse import quote
if len(params) == 0:
return ""
result = ""
# is a dictionary?
if type (params) is dict:
for key in params.keys():
newkey = quote (key)
if topkey != '':
newkey = topkey + quote('[' + key + ']')
if type(params[key]) is dict:
result += http_build_query (params[key], newkey)
elif type(params[key]) is list:
i = 0
for val in params[key]:
result += newkey + quote('[' + str(i) + ']') + "=" + quote(str(val)) + "&"
i = i + 1
# boolean should have special treatment as well
elif type(params[key]) is bool:
result += newkey + "=" + quote (str(int(params[key]))) + "&"
# assume string (integers and floats work well)
else:
result += newkey + "=" + quote (str(params[key])) + "&"
# remove the last '&'
if (result) and (topkey == '') and (result[-1] == '&'):
result = result[:-1]
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment