Skip to content

Instantly share code, notes, and snippets.

@lhns
Last active July 6, 2021 15:01
Show Gist options
  • Select an option

  • Save lhns/d3eea7c783b2b97c3d94995d72feeb16 to your computer and use it in GitHub Desktop.

Select an option

Save lhns/d3eea7c783b2b97c3d94995d72feeb16 to your computer and use it in GitHub Desktop.
similar to curl --fail-with-body
#!/bin/bash
curlFailWithBody() {
exec {fd}>&1
local httpCode="$(curl "$@" -w '%{http_code}' -o >(cat >&"$fd"))"
local exitCode="$?"
exec {fd}<&-
if (($exitCode == 0 && ($httpCode < 200 || $httpCode > 299))); then
exitCode=22
if (($httpCode >= 400 && $httpCode <= 555)); then
exitCode=$(($httpCode - 300))
fi
fi
return "$exitCode"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment