python - Reading json data line by line in Cherrypy -
i have json info going coming server in next format:
{"line":"one"} {"line":"two"} {"line":"three"}
while realize not valid json format have no command on how info reaching me. need able read info line line
now have simple cherrypy server setup take post request. here function handles post request:
class postevent(object): exposed = true def post(self, **urlparams): cl = cherrypy.request.headers['content-length'] raw_body = cherrypy.request.body.read(int(cl)) lines = raw_body.splitlines() open('log.txt', 'w') f: line in lines: f.write('%s\n' % line)
then issue next curl
command test:
curl -i -k -h "content-type: application/json" -h "accept: application/json" -x post --data @test_data -u username http://test-url.com
where file test_data
contains json info in format specified above. 200 response, however, of info read file on 1 line below:
{"line":"one"}{"line":"two"}{"line":"three"}
it seems if when cherrypy reading body ignoring line delimiters such \n
. how cherrypy read request body formatted? or more how can read request body line line , not @ once?
i cannot imagine cherrypy mangling info that.
your test write out newline count shows much more curl
not sending info newlines intact, , time request handler has newlines have been stripped (so raw_body.splitlines()
returns [raw_body]
resulting in 1 line beingness written).
make sure post --data-binary
switch; -d
defaults ascii , altering newlines you:
-d, --data
same --data-ascii
. post info purely binary, should instead utilize --data-binary
option.
python json rest curl cherrypy
No comments:
Post a Comment