Skip to main content

Get http without curl

Explanation

Script

Only works wi th HTTP not HTTPS

http_get.sh

#!/usr/bin/env bash

HOST="google.com"
PORT="80"

exec 3<>/dev/tcp/${HOST}/${PORT}

lines=(
'GET / HTTP/1.1'
'Host: google.com'
'Connection: Close'
''
)

printf '%s\r\n' "${lines[@]}" >&3

while read -r data <&3; do
echo "got server data: $data"
done

exec 3>&-

Result

./http_get.sh
got server data: HTTP/1.1 301 Moved Permanently
got server data: Location: http://www.google.com/
got server data: Content-Type: text/html; charset=UTF-8
got server data: Content-Security-Policy-Report-Only: object-src 'none';base-uri 'self';script-src 'nonce--w-3s0E4OvgXdsVAr7-yMA' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/other-hp
got server data: Date: Wed, 07 Aug 2024 15:59:06 GMT
got server data: Expires: Fri, 06 Sep 2024 15:59:06 GMT
got server data: Cache-Control: public, max-age=2592000
got server data: Server: gws
got server data: Content-Length: 219
got server data: X-XSS-Protection: 0
got server data: X-Frame-Options: SAMEORIGIN
got server data: Connection: close
got server data:
got server data: <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
got server data: <TITLE>301 Moved</TITLE></HEAD><BODY>
got server data: <H1>301 Moved</H1>
got server data: The document has moved
got server data: <A HREF="http://www.google.com/">here</A>.
got server data: </BODY></HTML>