Python requests module - POST failing - invalid character 'o' -
i trying convert raw curl command use python request module no luck. simple request query jboss mgmt interface not parse json correctly.
16:34:26,868 debug [org.jboss.as.domain.http.api] (httpmanagementservice-threads - 15) unable construct modelnode 'invalid character: o'
python version
python 2.7.6
working raw curl command:
/usr/bin/curl --digest -v -l -d - 'http://brenn:!12rori@localhost:9990/management' --header content-type:application/json '-d {"operation":"read-attribute","name":"server-state","json.pretty":1}'
in python code read in rest/curl payload
import requests ---- def readconfigfile(): open('jboss_modification.cfg') f: lines = f.readlines() return lines
the config file looks so
{"operation":"read-attribute","name":"server-state","json.pretty":1}
i convert str format readconfigfile() dictionary follows
def converttodictionary(incominglines): commands = [] lin in incominglines: #dumps = json.dumps(lin) obj = json.loads(lin) commands.append(obj) return commands
the python code execute request follows
def applyconfig(lines): url="http://localhost:9990/management" auth=httpbasicauth('brenn', '!12rori') s = requests.session() re=s.get(url, auth=httpdigestauth('brenn', '!12rori')) ##200 resp s.headers.update({'content-type': 'application/json'}) line in lines: payload=line r=s.post(url,payload) print(r.text)
any appreciated?
note: question has been updated few times resolved other issues....
the issues was...
initial json requestfailed because when read file python interpreted str.
converted dictionary using json.loads , server accepted request not parse json illegal character error
converted json str using json.dumps -- mind looks trying in first place -- , works
- read json file per
def readconfigfile():
above - convert json/dictionary per
def converttodictionary
above:json.loads(lin)
convert json "back" string using
json.dumps
, post followspayload = json.dumps(command) r = session.post(url, payload,auth=httpdigestauth('brenn', '!12rori')
)
Comments
Post a Comment