Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Created October 17, 2019 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carymrobbins/e2c65bb5d7e5dd23dacacc1b281f54da to your computer and use it in GitHub Desktop.
Save carymrobbins/e2c65bb5d7e5dd23dacacc1b281f54da to your computer and use it in GitHub Desktop.
Example of piping curl response body to jq, pretty-printing if it is JSON parseable, otherwise, outputting the raw response body. You can include additional information, like the HTTP version and response code.
req() {
curl -sS "$@" \
-Hcontent-type:application/json \
-w "HTTP/%{http_version} %{http_code}\n" \
-o >(jq -Rsr '. as $x | try fromjson catch $x')
}
% req httpbin.org/json
HTTP/1.1 200
{
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{
"title": "Wake up to WonderWidgets!",
"type": "all"
},
{
"items": [
"Why <em>WonderWidgets</em> are great",
"Who <em>buys</em> WonderWidgets"
],
"title": "Overview",
"type": "all"
}
],
"title": "Sample Slide Show"
}
}
% req httpbin.org/xml
HTTP/1.1 200
<?xml version='1.0' encoding='us-ascii'?>
<!-- A SAMPLE set of slides -->
<slideshow
title="Sample Slide Show"
date="Date of publication"
author="Yours Truly"
>
<!-- TITLE SLIDE -->
<slide type="all">
<title>Wake up to WonderWidgets!</title>
</slide>
<!-- OVERVIEW -->
<slide type="all">
<title>Overview</title>
<item>Why <em>WonderWidgets</em> are great</item>
<item/>
<item>Who <em>buys</em> WonderWidgets</item>
</slide>
</slideshow>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment