Created
September 30, 2024 18:26
-
-
Save perfectmak/23d65b74920ebd1a7e9a19fb4652aa0e to your computer and use it in GitHub Desktop.
Making an http request in bash (without CURL or Wget)
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
#!/bin/bash | |
#Sample usage: HOST=google.com ./tcp_request.sh | |
# Open a file descriptor 3 for input and output to tcp host and port 80 | |
exec 3<>/dev/tcp/$HOST/80 | |
lines=( | |
'GET /push_archive HTTP/1.1' | |
"Host: $HOST" | |
'Connection: close' | |
'' | |
) | |
# Write each HTTP header line to socket | |
printf '%s\r\n' "${lines[@]}" >&3 | |
# Read all output to stdout | |
while read -r data <&3; do | |
echo "got server data: $data" | |
done | |
# Close the file descriptor 3 | |
exec 3>&- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment