Created
January 14, 2021 21:51
-
-
Save alanbernstein/4725bdd2323fadef33cd78a02f6af2e4 to your computer and use it in GitHub Desktop.
curl+jq have a baby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# make an http request via curl | |
# try to decode it as json with jq | |
# if it works, pretty print the json | |
# if it doesn't, ugly print the response | |
F1="$HOME/tmp.curl" | |
F2="$HOME/tmp.jq" | |
curl -s $1 > $F1 | |
cat $F1 | jq . > $F2 2>&1 | |
error=$(head -1 $F2 | grep "parse error") | |
if [ "$error" == "" ]; then | |
cat $F2 | |
else | |
cat $F1 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment