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

Option to Pretty Print JSON Body #667

Closed
PSLLSP opened this issue Apr 2, 2018 · 2 comments
Closed

Option to Pretty Print JSON Body #667

PSLLSP opened this issue Apr 2, 2018 · 2 comments

Comments

@PSLLSP
Copy link

PSLLSP commented Apr 2, 2018

There is closed issue #410.
I still miss an option to force httpie to format body in the answer. I see two cases:

  1. Misconfigured server. Some REST API services are misconfigured, they report JSON formatted data but with wrong type, like text/plain or text/html or anything else. I cannot do anything about that.

  2. New content types could return JSON but until these are known by httpie, data are not formated. For example, application/dns-json

$ http --version
0.8.0

$ http 'https://cloudflare-dns.com/dns-query?ct=application/dns-json&name=github.com&type=A'
HTTP/1.1 200 OK
CF-RAY: 405571cf6e0d2b4c-AMS
Connection: keep-alive
Content-Length: 276
Content-Type: application/dns-json
Date: Mon, 02 Apr 2018 18:44:03 GMT
Server: cloudflare-nginx
Set-Cookie: __cfduid=d649572db5862d7d87ba5b2998aaac9191522694643; expires=Tue, 02-Apr-19 18:44:03 GMT; path=/; domain=.cloudflare-dns.com; HttpOnly; Secure
cache-control: max-age=12

{"Status": 0,"TC": false,"RD": true, "RA": true, "AD": false,"CD": false,"Question":[{"name": "github.com.", "type": 1}],"Answer":[{"name": "github.com.", "type": 1, "TTL": 12, "data": "192.30.253.112"},{"name": "github.com.", "type": 1, "TTL": 12, "data": "192.30.253.113"}]}

Example of misconfigured service, it reports JSON data with context text/html
$ http 'https://www.coinotron.com/app?action=api'

  1. It could be useful to have an option to disable formating (to force text format); it could be useful for troubleshooting or for scripts. I have to use curl in some cases, to see 'raw' data sent by server.
$ http --body http://now.httpbin.org
{
    "now": {
        "epoch": 1522697883.2888725, 
        "iso8601": "2018-04-02T19:38:03.288872Z", 
        "rfc2822": "Mon, 02 Apr 2018 19:38:03 GMT", 
        "rfc3339": "2018-04-02T19:38:03.28Z", 
        "slang_date": "today", 
        "slang_time": "now"
    }, 
    "urls": [
        "/", 
        "/docs", 
        "/when/:human-timestamp", 
        "/parse/:machine-timestamp"
    ]
}

$ curl http://now.httpbin.org
{"now": {"epoch": 1522697885.5953357, "slang_date": "today", "slang_time": "now", "iso8601": "2018-04-02T19:38:05.595336Z", "rfc2822": "Mon, 02 Apr 2018 19:38:05 GMT", "rfc3339": "2018-04-02T19:38:05.59Z"}, "urls": ["/", "/docs", "/when/:human-timestamp", "/parse/:machine-timestamp"]}
@jonchun
Copy link

jonchun commented Apr 25, 2018

Looks like this is already supported if you set the correct flags. Relevant excerpt:

  --json, -j
      (default) Data items from the command line are serialized as a JSON object.
      The Content-Type and Accept headers are set to application/json
      (if not specified).

  --pretty {all,colors,format,none}
      Controls output processing. The value can be "none" to not prettify
      the output (default for redirected output), "all" to apply both colors
      and formatting (default for terminal output), "colors", or "format".

The --json flag seems to be documented poorly, but you can see that it also explicitly sets the encoding to json here.

~ $ http --version
0.9.9
~ $ http --pretty=all --body --json http://0.0.0.0:8000/data.txt
HTTP/1.0 200 OK
Content-Length: 29
Content-type: text/plain
Date: Wed, 25 Apr 2018 12:43:48 GMT
Last-Modified: Wed, 25 Apr 2018 12:43:26 GMT
Server: SimpleHTTP/0.6 Python/3.6.5

{
    "foo": "bar!",
    "test": "yay"
}
~ $ http --body --pretty=none http://now.httpbin.org
{"now": {"epoch": 1524659042.818514, "slang_date": "today", "slang_time": "now", "iso8601": "2018-04-25T12:24:02.818514Z", "rfc2822": "Wed, 25 Apr 2018 12:24:02 GMT", "rfc3339": "2018-04-25T12:24:02.81Z"}, "urls": ["/", "/docs", "/when/:human-timestamp", "/parse/:machine-timestamp"]}

@jkbrzt
Copy link
Member

jkbrzt commented Nov 6, 2018

This should work fine with HTTPie v0.9.4+.

screen shot 2018-11-06 at 11 31 07

@jkbrzt jkbrzt closed this as completed Nov 6, 2018
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

3 participants