-
-
Save jatindhankhar/0c1d6c5b2d3f7c7520c4b4afba916cfc 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 hidden or 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 . | |
#} | |
## Updated to add more output control. | |
## Some weird sed expressions cobbled together. Not a sed expert, if you know of a more simple or elegant way, let me know :) | |
response=$( curl -sS -i https://www.metaweather.com/api/location/search/\?query\=new -w "\n\n%{time_connect},%{time_total},%{speed_download},%{http_code},%{size_download},%{url_effective}\n") | |
# Print headers | |
echo "$response" | sed "/^\s*$(printf '\r')*$/q" | |
# Print json | |
echo "$response" | sed "$,/^\s$(printf '\r')*$/d" | sed "1,/^\s*$(printf '\r')*$/d" | jq '.' | |
# Print extra stats | |
echo "$response" | sed "1,/^\s*$(printf '\r')*$/d" | sed "1,/^\s*$(printf '\r')*$/d" | |
## Output | |
Headers
echo "$response" | sed "/^\s*$(printf '\r')*$/q"
HTTP/2 200
x-xss-protection: 1; mode=block
content-language: en
x-content-type-options: nosniff
strict-transport-security: max-age=2592000; includeSubDomains
vary: Accept-Language, Cookie
allow: GET, HEAD, OPTIONS
x-frame-options: DENY
content-type: application/json
x-cloud-trace-context: dadc16f49f1b268fc3ddffceefb8d189
date: Thu, 21 Jun 2018 12:03:07 GMT
server: Google Frontend
content-length: 475
Json
echo "$response" | sed "$,/^\s$(printf '\r')*$/d" | sed "1,/^\s*$(printf '\r')*$/d" | jq '.'
[
{
"title": "New York",
"location_type": "City",
"woeid": 2459115,
"latt_long": "40.71455,-74.007118"
},
{
"title": "New Delhi",
"location_type": "City",
"woeid": 28743736,
"latt_long": "28.643999,77.091003"
},
{
"title": "New Orleans",
"location_type": "City",
"woeid": 2458833,
"latt_long": "29.953690,-90.077713"
},
{
"title": "Newcastle",
"location_type": "City",
"woeid": 30079,
"latt_long": "54.977940,-1.611620"
},
{
"title": "Newark",
"location_type": "City",
"woeid": 2459269,
"latt_long": "40.731972,-74.174179"
}
]
Stats
echo "$response" | sed "1,/^\s*$(printf '\r')*$/d" | sed "1,/^\s*$(printf '\r')*$/d"
0.171251,0.828936,573.000,200,475,https://www.metaweather.com/api/location/search/?query=new
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output