httpie / cli

🥧 HTTPie CLI — modern, user-friendly command-line HTTP client for the API era. JSON support, colors, sessions, downloads, plugins & more.
https://httpie.io
BSD 3-Clause "New" or "Revised" License
33.37k stars 3.67k forks source link

Force result to be formated as json pretty #349

Closed cmoulliard closed 8 years ago

cmoulliard commented 9 years ago

Is there a workaround to force httpie to pretty the body of the http result as json format (indent, ...)?

As the code https://github.com/jakubroztocil/httpie/blob/master/httpie/output/formatters/json.py#L13 the mime-type of the response, such a response is not well formatted if the response does not contain json as mime-type

http --session=admin -j --pretty=all --verbose http://localhost:8181/hawtio/jolokia/list/java.lang/type=Memory
GET /hawtio/jolokia/list/java.lang/type=Memory HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Authorization: Basic YWRtaW46YWRtaW4=
Connection: keep-alive
Content-Type: application/json
Cookie: JSESSIONID=1e4hs0o1sidfc4edui24da7jb
Host: localhost:8181
User-Agent: HTTPie/0.9.2

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Content-Length: 681
Content-Type: text/plain;charset=UTF-8
Date: Mon, 04 May 2015 13:10:13 GMT
Expires: Mon, 04 May 2015 12:10:13 GMT
Pragma: no-cache
Server: Jetty(8.1.16.v20140903)
X-Frame-Options: SAMEORIGIN

{"timestamp":1430745013,"status":200,"request":{"path":"java.lang\/type=Memory","type":"list"},"value":{"desc":"Information on the management interface of the MBean","op":{"gc":{"ret":"void","desc":"gc","args":[]}},"attr":{"Verbose":{"desc":"Verbose","type":"boolean","rw":true},"ObjectPendingFinalizationCount":{"desc":"ObjectPendingFinalizationCount","type":"int","rw":false},"NonHeapMemoryUsage":{"desc":"NonHeapMemoryUsage","type":"javax.management.openmbean.CompositeData","rw":false},"HeapMemoryUsage":{"desc":"HeapMemoryUsage","type":"javax.management.openmbean.CompositeData","rw":false},"ObjectName":{"desc":"ObjectName","type":"javax.management.ObjectName","rw":false}}}}

Result should be displayed as such :

{
  "timestamp": 1430745013,
  "status": 200,
  "request": {
    "path": "java.lang\/type=Memory",
    "type": "list"
  },
  "value": {
    "desc": "Information on the management interface of the MBean",
    "op": {
      "gc": {
        "ret": "void",
        "desc": "gc",
        "args": []
      }
    },
    "attr": {
      "Verbose": {
        "desc": "Verbose",
        "type": "boolean",
        "rw": true
      },
      "ObjectPendingFinalizationCount": {
        "desc": "ObjectPendingFinalizationCount",
        "type": "int",
        "rw": false
      },
      "NonHeapMemoryUsage": {
        "desc": "NonHeapMemoryUsage",
        "type": "javax.management.openmbean.CompositeData",
        "rw": false
      },
      "HeapMemoryUsage": {
        "desc": "HeapMemoryUsage",
        "type": "javax.management.openmbean.CompositeData",
        "rw": false
      },
      "ObjectName": {
        "desc": "ObjectName",
        "type": "javax.management.ObjectName",
        "rw": false
      }
    }
  }
}
drobati commented 9 years ago

I believe this is of interest to me because I also have a Content-Type that passes valid json, but our request is not set to Content-Type: json; can't say what the value really is. I didn't see a way to force the output to be pretty printed. Our Content-Type value is necessary but returns valid json responses.

imacleod commented 9 years ago

@cmoulliard and @drobati If you can provide a publicly available endpoint with this type of response that would be helpful.

jcrumb commented 9 years ago

I have a potential fix for this, will send a PR after some additional testing:

(venv)jay@ubuntu:~/git/httpie$ http :8000/
HTTP/1.1 200 OK
Content-Length: 34
Content-Type: text/plain;charset=UTF-8
Date: Thu, 02 Jul 2015 05:29:07 GMT

{"hello":"world","testing":"json"}

(venv)jay@ubuntu:~/git/httpie$ http :8000/ --force-mime=text/json
HTTP/1.1 200 OK
Content-Length: 34
Content-Type: text/plain;charset=UTF-8
Date: Thu, 02 Jul 2015 05:29:10 GMT

{
    "hello": "world", 
    "testing": "json"
}
kfix commented 8 years ago

@jcrumb opened his PR in #368

drobati commented 8 years ago

:+1: @kfix

Couldn't provide a public link as it was a private API. Looking forward to this merge.

fefas commented 8 years ago

+1

jkbrzt commented 8 years ago

Now with the --json, -j option HTTPie tries to detect JSON responses when the Content-Type is unknown or text/plain.

drobati commented 8 years ago

Great work @jcrumb and @jkbrzt! :clap: :clap: :clap:

danielbodart commented 6 years ago

Ideally if you specify --json it should just override the content type completely. For example this public API returns "text/html;charset=UTF-8" for a json resource

http https://api.nicehash.com/api?method=stats.global.current

Which is obviously wrong (from the resource perspective) but it would seem the setting --json should just override the returned content type.

Thanks