Created
August 29, 2015 18:30
-
-
Save cirla/1c0411c1dc1bb2fe0e9f to your computer and use it in GitHub Desktop.
Bash function to curl an HTTP JSON API, print the headers, and then print the body formatted using jq
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
# Bash function to curl an HTTP JSON API, print the headers, | |
# and then print the body formatted using jq | |
# | |
# Example usage: jcurl https://api.github.com/users/cirla | |
function jcurl() { | |
# disable progress bar and print headers to stdout | |
response=$(curl -sD - $@) | |
# split response at the first empty line (ignoring whitespace) | |
# sed on OS X neither recognizes \r as a special character nor matches | |
# carriage returns with \s, so we have to insert a carriage return using printf | |
echo "$response" | sed "/^\s*$(printf '\r')*$/q" | |
echo "$response" | sed "1,/^\s*$(printf '\r')*$/d" | jq . | |
} |
Or use curl -v, which sends the verbose output to stdin and actual data to stdout
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or just
brew install httpie
and save yourself the time dealing with corner cases.