0

given json {"foo":"bazz","1":2}

I want to convert it to POST data :

"foo"="bazz";"1"=2;

(the data format in case it were posted from html form)

is there any exist decoder for json>> POST data? if no, does next script will do it as well?

json_body = {"foo":"bazz","1":2}
data = ''
for key, value in json_body.items():
    data += '"{key}"={value};'.format(key=key, value=value)
print data
>> "foo"="bazz";"1"=2;

thanks

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
eligro
  • 785
  • 3
  • 13
  • 23

1 Answers1

4

Use urllib.parse.urlencode:

from urllib.parse import urlencode

data = urlencode(json_body)

This produces x-www-form-urlencoded data, which is the default mime-type used by browsers when POST-ing HTML forms.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343