python - Difficulty converting string to JSON object in order to urlencode it -


i'm having trouble converting string built on script json object (i'm trying build post data make post request api endpoint).

i'm getting following error:

bos-mpqpu:config_parse rabdelaz$ python3 lexparse.py {"__class__":"httptestclient","description":"cp codes","url":"://","serverip":"","method":"get","enabled":true,"headers":[],"tests":[],"purge":false,"procs":[]} traceback (most recent call last):   file "lexparse.py", line 448, in <module> print(ast.literal_eval(test_case_act_template.substitute({'protocol': "",'host': "", 'path' : "", 'query' : "", 'serverip' : "", 'tests' : ""})))   file "/library/frameworks/python.framework/versions/3.6/lib/python3.6/ast.py", line 85, in literal_eval return _convert(node_or_string)   file "/library/frameworks/python.framework/versions/3.6/lib/python3.6/ast.py", line 66, in _convert     in zip(node.keys, node.values))   file "/library/frameworks/python.framework/versions/3.6/lib/python3.6/ast.py", line 65, in <genexpr>     return dict((_convert(k), _convert(v)) k, v   file "/library/frameworks/python.framework/versions/3.6/lib/python3.6/ast.py", line 84, in _convert     raise valueerror('malformed node or string: ' + repr(node)) valueerror: malformed node or string: <_ast.name object @ 0x103c50c18> 

here code generates above:

test_case_act_template = template('{"__class__":"httptestclient","description":"cp codes","url":"$protocol://$host$path$query","serverip":"$serverip","method":"get","enabled":true,"headers":[],"tests":[$tests],"purge":false,"procs":[]}')   print(test_case_act_template.substitute({'protocol': "",'host': "", 'path' : "", 'query' : "", 'serverip' : "", 'tests' : ""}))  print(ast.literal_eval(test_case_act_template.substitute({'protocol': "",'host': "", 'path' : "", 'query' : "", 'serverip' : "", 'tests' : ""}))) 

notice first print statement results in output line starts {"__class__"

my overall objective urlencode json string searching on lead me believe should first ast.literal_eval before urlencoding it.

the end goal looks this:

form_data = {'name' : 'cp code demo', 'configfilename' : '1-dr4e2_kona-ion.aws-eu-ssl.template_pm-2', 'type' : 'json', 'script' : urlencode(json.load(post_data))} 

where post_data series of template substitutions.

the original error lead me start looking ast.literal_eval follows:

   traceback (most recent call last):    file "lexparse.py", line 521, in <module> form_data = {'name' : 'cp code demo', 'configfilename' : '1-dr4e2_kona-ion.aws-eu-ssl.template_pm-2', 'type' : 'json', 'script' : urlencode(post_data)}    file "/library/frameworks/python.framework/versions/3.6/lib/python3.6/urllib/parse.py", line 862, in urlencode "or mapping object").with_traceback(tb)     file "/library/frameworks/python.framework/versions/3.6/lib/python3.6/urllib/parse.py", line 854, in urlencode   raise typeerror typeerror: not valid non-string sequence or mapping object 

just in line question, template seemingly produces valid json parse python dict , give urllib.parse.urlencode() turn valid url params, like:

import json urllib.parse import urlencode string import template  your_template = template('{"__class__":"httptestclient","description":"cp codes",'                          '"url":"$protocol://$host$path$query","serverip":"$serverip",'                          '"method":"get","enabled":true,"headers":[],"tests":[$tests],'                          '"purge":false,"procs":[]}')  post_data = your_template.substitute({'protocol': "",                                       'host': "",                                       'path': "",                                       'query': "",                                       'serverip': "",                                       'tests': ""})  form_data = {'name': 'cp code demo',              'configfilename': '1-dr4e2_kona-ion.aws-eu-ssl.template_pm-2',              'type': 'json',              'script': urlencode(json.loads(post_data))}  print(form_data) 

then can feed whatever want. i'd still advise on solving problem have certificates offsetting curl - issues might experience when trying pass large amount of data via cli can overcome issues you've been experiencing certificates.


Comments

Popular posts from this blog

Ansible warning on jinja2 braces on when -

Parsing a protocol message from Go by Java -

node.js - Node js - Trying to send POST request, but it is not loading javascript content -