Skip to main content

HTTP Request with Bash

·162 words·1 min
Posts bash cli http
zd
Author
zd
cli-geek, strategist
Table of Contents
This is an augment on writing BASH script to perform HTTP request (without using curl).

In the last posts, “Creating TCP/UDP Socket with /bin/bash”, I’ve shown 4 usages on creating TCP socket using /bin/bash.

Creating TCP/UDP Socket with /bin/bash
·415 words·2 mins
Posts bash cli tcpip
Using /bin/bash to create TCP/UDP socket for troubleshooting

Here, I’ll be showing a more comprehensive way to perform HTTP request with Bash script, including return HTTP headers.

#!/usr/bin/env bash

PORT=80
HOST="httpbin.org"

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

HDRS=(
    'GET /ip HTTP/1.1'
    'Host: $HOST'
    'Connection: close'
    ''
)

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

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

exec 3>&-

And here is the output.

$ ./bash_curl.sh

recv: HTTP/1.1 200 OK
recv: Date: Thu, 22 Aug 2024 15:14:54 GMT
recv: Content-Type: application/json
recv: Content-Length: 33
recv: Connection: close
recv: Server: gunicorn/19.9.0
recv: Access-Control-Allow-Origin: *
recv: Access-Control-Allow-Credentials: true
recv:
recv: {
recv: "origin": "115.164.33.158"
recv: }

$

Links#

Related

Insecurity in HTTP Headers
·2195 words·11 mins
Posts Essential async cli http python
Based on essential security, here is how to protect users by securing HTTP headers for a website.
HTTP Versioning
·740 words·4 mins
Posts async cli http python tools
HTTP/1 vs HTTP/1.1 vs HTTP/2 vs HTTP/3
Creating TCP/UDP Socket with /bin/bash
·415 words·2 mins
Posts bash cli tcpip
Using /bin/bash to create TCP/UDP socket for troubleshooting