Software Engineering :: Source Control :: VCS :: Git :: Managed Hosting Platform :: GitHub :: Gist :: API :: Scripting :: gist-list.sh
⪼ Made with 💜 by Polyglot.
#!/bin/bash
# Load environment variables from .env file
export $(grep -v '^#' .env | xargs)
# Initial URL
url="https://api.github.com/gists?per_page=100"
# Loop through pages
while [ "$url" != "" ]; do
# Make the curl request and capture headers and body
response=$(curl -s -D - --request GET \
--url "$url" \
--header 'Accept: application/vnd.github+json' \
--header "Authorization: Bearer ${GIST_API_KEY}" \
--header 'X-GitHub-Api-Version: 2022-11-28')
# Extract the body of the response
body=$(echo "$response" | sed -n '/^\r$/,$p' | sed '1d')
# Process the body (e.g., save it to a file, print it, etc.)
echo "$body"
# Extract the Link header for pagination
link_header=$(echo "$response" | grep -i "^Link:")
# Parse the next page URL from the Link header
url=$(echo "$link_header" | sed -n 's/.*<\(.*\)>; rel="next".*/\1/p')
# If no next URL is found, exit the loop
if [ -z "$url" ]; then
url=""
fi
done