Skip to content

Commit

Permalink
fix output issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mitnk committed Apr 18, 2019
1 parent 7894691 commit 582aefa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
45 changes: 22 additions & 23 deletions curljson.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import subprocess
import fileinput
import json
import subprocess
import sys
import fileinput


def get_cmd_output(cmd):
Expand Down Expand Up @@ -33,36 +33,35 @@ def main():
stream_func = output.strip().split
stream_args = ['\n', ]

response_spliter_found = False
raw_lines = []
json_str = ''
header_lines = []
body_str = ''
body_lines = []
http_version_found = False

for line in stream_func(*stream_args):
line = line.strip()
if len(line.replace('\r', '')) == 0:
response_spliter_found = True
raw_lines.append('\n')
elif line.startswith(r'HTTP/'):
response_spliter_found = False
if line.startswith(r'HTTP/'):
http_version_found = True

if http_version_found and not line.replace('\r', ''):
header_lines.append('\n')
http_version_found = False

if response_spliter_found:
json_str += line
if http_version_found:
header_lines.append(line)
else:
raw_lines.append(line)
body_str += line
body_lines.append(line)

if response_spliter_found:
for line in raw_lines:
if line == '\n':
print('')
else:
print(line)
else:
json_str = '\n'.join(raw_lines)
if header_lines:
headers = '\n'.join(header_lines).replace('\n\n', '\n')
print(headers)

try:
result = json.loads(json_str)
result = json.loads(body_str)
except ValueError:
print(json_str)
body = '\n'.join(body_lines)
print(body.strip())
else:
print(json.dumps(result, indent=4, sort_keys=True))

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

setup(
name='curljson',
version='0.9.8',
version='0.9.9',

description='curl with pretty json outputs',
long_description=DESC,
Expand Down

0 comments on commit 582aefa

Please sign in to comment.