jonaslu / ain

A HTTP API client for the terminal
MIT License
601 stars 13 forks source link

Response processing? #8

Closed matschaffer closed 3 years ago

matschaffer commented 3 years ago

I gave this a try for some elasticsearch querying work but couldn't find anything that'd let me do response processing.

For simple stuff I can pipe to jq just fine, but it might be nice to be able to encode something like jq .hits.total.value into the ain file to get just that portion of the response, or indented JSON response output.

If it's there and I missed it let me know, but seems like for now pipe is the only option.

jonaslu commented 3 years ago

Reply

Response processing is not part of ain simply because there are other tools that do this way better - like jq. Connecting them in a pipe is the way to go (sticking with the linux principle of doing one thing well).

But you can solve your problem by either creating a shell-function:

gethits () { ain "$@" | jq .hits.total.value }

And call it as you would with ain - gethits template1.ain template2.ain etc

Or create a shell-file, like gethits.sh, make it executable and put it on your $PATH, then same as above

#!/bin/sh
ain "$@" | jq .hits.total.value

As for the pretty-printing, httpie should pretty print json-responses by default, or you can create another bash-function where you pipe it into jq.

Hope that helps!

matschaffer commented 3 years ago

Cool, thanks for the clarification! I'll continue the pipe approach.