Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty Print JSON Body #410

Closed
loretoparisi opened this issue Nov 13, 2015 · 6 comments
Closed

Pretty Print JSON Body #410

loretoparisi opened this issue Nov 13, 2015 · 6 comments

Comments

@loretoparisi
Copy link

How to pretty print a JSON output body in order to format it in bash console like

macbookproloreto:~ admin$ echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
{
    "bar": "ipsum",
    "foo": "lorem"
}

My command looks like now

http --session=cookies.txt --pretty all --json --print=b --style=xcode "https://myapi.company.com | python -m json.tool

Does the option --pretty aims to pretty print the output body? Having it seems to not prettify the output body, instead it prints the json as plain text

'{"foo": "lorem", "bar": "ipsum"}'
@jkbrzt
Copy link
Member

jkbrzt commented Jan 1, 2016

This works out of the box:

screen shot 2016-01-01 at 6 58 34 pm

@jkbrzt jkbrzt closed this as completed Jan 1, 2016
@loretoparisi
Copy link
Author

@jkbrzt yes but this is not the response body. It seems it does not pretty the body but only the headers.

@jkbrzt
Copy link
Member

jkbrzt commented Jan 3, 2016

@loretoparisi it is the response body, see http://httpbin.org/get

@loretoparisi
Copy link
Author

Ok I have found the issue, it's something wrong in the response headers. In fact if I try a well formatted response body with json format like:

http --session=cookies.txt --pretty all --json --print=b --style=xcode "https://api.spotify.com/v1/tracks/0eGsygTp906u18L0Oimnem"

it works.
When the http response headers are text/plain

HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Length: 1996
Content-Type: text/plain; charset=utf-8

the resulting formatting is plain.

@jkbrzt
Copy link
Member

jkbrzt commented Jan 3, 2016

HTTPie looks at the response Content-Type header. If it's text/plain, then it doesn't do any special formatting. The URL you provided returns Content-Type: application/json; charset=utf-8 so it gets properly formatted and highlighted as JSON.

screen shot 2016-01-03 at 6 58 33 pm

@loretoparisi
Copy link
Author

@jkbrzt exactly. In fact the issue was that the server headers was text/plain while the body was a text formatted as a json body (well formatted json). Basically the response header Content-Type is not application/json. Can HTTPie force json formatting in those cases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants