Last active
April 21, 2024 08:56
-
-
Save KShivendu/a6f2c4dd0bee01847d7e8736aa1c4f1c to your computer and use it in GitHub Desktop.
kurl: Curl simplified
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
#!/bin/bash | |
# Simplified curl | |
function kurl() { | |
local delimiter=$'\x1E' # ASCII record separator | |
local response=$(curl -s "$@" -w "${delimiter}%{http_code}") | |
IFS="$delimiter" read -r body http_code <<< "$response" | |
export HTTP_CODE="$http_code" | |
echo $body | |
local field_count=$(awk -F"$delimiter" '{print NF}' <<< "$body") | |
if (( field_count != 2 )); then | |
echo "Error: The response body contains the delimiter." | |
return 1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
My assumptions: