Skip to content

Instantly share code, notes, and snippets.

@calilisantos
Last active April 27, 2025 14:18
Show Gist options
  • Select an option

  • Save calilisantos/87a07d5cf409eb7422a739edc6afd627 to your computer and use it in GitHub Desktop.

Select an option

Save calilisantos/87a07d5cf409eb7422a739edc6afd627 to your computer and use it in GitHub Desktop.
Use cases of curl in shell and DevOps
<#
CLIENT FOR URL (curl) POWERSHELL SCRIPT USE CASES
1. RUNNING THE SCRIPT:
.\client_for_url.ps1
2. RUNNING THE SCRIPT WITH DEBUGGING:
Set-PSDebug -Trace 1
Some curl flags and examples: https://reqbin.com/req/c-skhwmiil/curl-flags-example
#>
###################
Write-Host "[INFO] 1. FILE DOWNLOAD WITH curl. Useful for dependencies updates, vulnerability checks, etc."
$FileUrl = "https://repo1.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/3.5.3/mariadb-java-client-3.5.3.jar"
$OutputFile = "mariadb-java-client.jar"
curl -s -o $OutputFile $FileUrl
Write-Host "[INFO] List of files in the current directory:"
Get-ChildItem
###################
Write-Host "`n[INFO] 2. API REQUEST WITH curl. Bye Postman!"
$ApiUrl = "https://jsonplaceholder.typicode.com/posts"
$Headers = @{ "Content-Type" = "application/json" }
Write-Host "[INFO] 2.1 GET REQUEST"
$getResponse = curl -s $ApiUrl | ConvertFrom-Json
$getResponse[0..1] | ConvertTo-Json -Depth 3
Write-Host "`n[INFO] 2.2 POST REQUEST"
$postBody = '{"title": "new post", "body": "my new post", "userId": 1}'
$postResponse = curl -s -X POST -H "Content-Type: application/json" -d $postBody $ApiUrl
$postResponse
Write-Host "`n[INFO] 2.3 PUT REQUEST"
$putBody = '{"title": "new post", "body": "my new post", "userId": 1, "id": 1}'
$putResponse = curl -s -X PUT -H "Content-Type: application/json" -d $putBody "$ApiUrl/1"
$putResponse
Write-Host "`n[INFO] 2.4 DELETE REQUEST"
$deleteResponse = curl -s -X DELETE "$ApiUrl/1"
$deleteResponse
###################
Write-Host "`n[INFO] 3. WEB PAGE HEALTH CHECK. Check if the page is up and running (and your scraper too!)."
$NameToTest = "Calili Santos"
$PropertyToTest = 'itemprop="name"'
$Url = "https://github.com/calilisantos"
$htmlFlat = (curl -s $Url) -join ""
$pattern = "$PropertyToTest>\s*$NameToTest\s*</span>"
if ($htmlFlat -match $pattern) {
Write-Host "[INFO] Name '$NameToTest' found in $Url"
} else {
Write-Host "[ERROR] '$NameToTest' not found in $Url"
exit 1
}
: <<'START_COMMENT'
CLIENT FOR URL (curl) SHELL SCRIPT USE CASES
1. GIVING SCRIPT EXECUTE PERMISSION:
chmod +x client_for_url.sh
2. RUNNING THE SCRIPT:
./client_for_url.sh
3. RUNNING THE SCRIPT WITH DEBUGGING:
bash -xv client_for_url.sh
some curl flags: https://reqbin.com/req/c-skhwmiil/curl-flags-example
START_COMMENT
###################
echo "[INFO] 1. FILE DOWNLOAD WITH curl. Util for dependecies updates, vulnerability checks, etc."
FILE_URL="https://repo1.maven.org/maven2/org/mariadb/jdbc/mariadb-java-client/3.5.3/mariadb-java-client-3.5.3.jar"
OUTPUT_FILE="mariadb-java-client.jar"
curl -s -o "$OUTPUT_FILE" "$FILE_URL" # -s for silent mode, -o to specify output file
echo "[INFO] List of files in the current directory:"
ls
###################
echo "[INFO] 2. API REQUEST WITH curl. Bye Postman!"
API_URL="https://jsonplaceholder.typicode.com/posts"
HEADERS="Content-Type: application/json"
echo "[INFO] 2.1 GET REQUEST"
get_response=$(curl -s "$API_URL" | jq '.[0:2]') # get the first 2 posts with jq
echo "[INFO] get_response" $get_response
echo "[INFO] 2.2 POST REQUEST"
post_body='{"title": "new post", "body": "my new post", "userId": 1}'
post_response=$(curl -s -X POST -H "$HEADERS" -d "$post_body" "$API_URL") # -X specifies the request method, -H specifies headers, -d specifies data
echo "[INFO] post_response" $post_response
echo "[INFO] 2.3 PUT REQUEST"
put_body='{"title": "new post", "body": "my new post", "userId": 1, "id": 1}'
put_response=$(curl -s -X PUT -H "$HEADERS" -d "$put_body" "$API_URL/1")
echo "[INFO] put_response" $put_response
echo "[INFO] 2.4 DELETE REQUEST"
delete_response=$(curl -s -X DELETE "$API_URL/1")
echo "[INFO] delete_response" $delete_response
###################
echo "[INFO] 3. WEB PAGE HEALTH CHECK. Check if the page is up and running (and your scraper too!)."
NAME_TO_TEST="Calili Santos"
PROPERTY_TO_TEST="itemprop=\"name\"" # Backslash (\) used to escape the double quotes inside the string
: <<'CONTENT_EXPECTED_REGEX_COMMENT'
CONTENT_EXPECTED_REGEX will search for the name in the HTML using grep
\s* matches any amount of whitespace (spaces, tabs, newlines)
CONTENT_EXPECTED_REGEX_COMMENT
CONTENT_EXPECTED_REGEX="$PROPERTY_TO_TEST>\s*$NAME_TO_TEST\s*"
URL="https://github.com/calilisantos"
HTML_FLAT=$(curl -s "$URL" | tr -d '\n') # tr -d '\n' removes newlines from the HTML content
if ! echo "$HTML_FLAT" | grep -Pq "$CONTENT_EXPECTED_REGEX"; then
# grep -Pq: -P enables Perl-compatible regex, -q suppresses output; grep: global regular expression print
echo "[ERROR] $NAME_TO_TEST not found in $URL"
exit 1
fi
echo "[INFO] Name $NAME_TO_TEST found in $URL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment