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

Ability to filter JSON or XML #291

Closed
hrj opened this issue Jan 5, 2015 · 5 comments
Closed

Ability to filter JSON or XML #291

hrj opened this issue Jan 5, 2015 · 5 comments

Comments

@hrj
Copy link

hrj commented Jan 5, 2015

Sometimes API responses contain a lot of data.

Given an endpoint that returns JSON or XML, it would be cool to be able to do something like

http http://myapi.endpoint/ -q "abc/xyz"

and only get to see the "abc/xyz" sub-tree of the response.

@rose
Copy link

rose commented Jan 7, 2015

+1, this would be awesome

@corpix
Copy link

corpix commented Jan 7, 2015

There are some external tools for this:
  - jq for JSON http://stedolan.github.io/jq/
  - xmlstarlet and others for XML http://stackoverflow.com/a/15461774
Why not simply use them?

@hrj
Copy link
Author

hrj commented Jan 7, 2015

@corpix Thanks for the links; wasn't aware of them.

There are pros and cons to an integrated tool. I am on the fence about this; I will leave it to the developers to close this issue if they wish.

@jkbrzt
Copy link
Member

jkbrzt commented Jan 7, 2015

I'm afraid this is slightly beyond the scope of what httpie should support. jq for JSON is excellent and integrates well with HTTPie via pipes. I'm planing on adding some examples to the docs, but here is a simple one for now:

$ http httpbin.org/get
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 398
Content-Type: application/json
Date: Wed, 07 Jan 2015 19:03:04 GMT
Server: gunicorn/18.0
Via: 1.1 vegur

{
    "args": {},
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Connect-Time": "1",
        "Connection": "close",
        "Host": "httpbin.org",
        "Total-Route-Time": "1",
        "User-Agent": "HTTPie/0.8.0",
        "Via": "1.1 vegur",
        "X-Request-Id": "2b2e03a0-0449-40f1-9027-73bfe9edf31f"
    },
    "url": "http://httpbin.org/get"
}
$ http httpbin.org/get | jq '.headers["User-Agent"]'
"HTTPie/0.8.0"

And other tools exist for querying different formats.

@jkbrzt jkbrzt closed this as completed Jan 7, 2015
@msabramo
Copy link
Contributor

Here's another example of using jq; this one happens to also use httpie-unixsocket to communicate with Docker over /var/run/docker.sock:

$ http http+unix://%2Fvar%2Frun%2Fdocker.sock/info | jq .RegistryConfig
{
  "InsecureRegistryCIDRs": [
    "127.0.0.0/8"
  ],
  "IndexConfigs": {
    "docker.io": {
      "Name": "docker.io",
      "Mirrors": null,
      "Secure": true,
      "Official": true
    }
  },
  "Mirrors": null
}

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

5 participants