Created
October 17, 2019 18:48
-
-
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.
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
req() { | |
curl -sS "$@" \ | |
-Hcontent-type:application/json \ | |
-w "HTTP/%{http_version} %{http_code}\n" \ | |
-o >(jq -Rsr '. as $x | try fromjson catch $x') | |
} |
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
% 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