Skip to content

Instantly share code, notes, and snippets.

@cirla
Created August 29, 2015 18:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cirla/1c0411c1dc1bb2fe0e9f to your computer and use it in GitHub Desktop.
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
# 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 .
}
@cirla
Copy link
Author

cirla commented Aug 31, 2015

Or just brew install httpie and save yourself the time dealing with corner cases.

@logsnaath
Copy link

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