𝗠𝗮𝗸𝗲 𝗛𝗧𝗧𝗣 𝗥𝗲𝗾𝘂𝗲𝘀𝘁𝘀 𝗪𝗶𝘁𝗵𝗼𝘂𝘁 𝗖𝘂𝗿𝗹
You might work on a server without curl or wget. You need to test an endpoint but those tools are missing. You can use Bash to send HTTP requests directly.
Bash has a hidden feature called /dev/tcp. This is not a real file. It is a special device Bash understands. When you write to /dev/tcp/hostname/port, Bash opens a TCP connection for you.
How it works:
You use the exec command to manage file descriptors. For example, you can open file descriptor 3 for reading and writing.
The syntax looks like this: exec 3<>/dev/tcp/example.com/80
Once this connection is open, you send data to it. You also read data from it. This method shows you how the HTTP protocol works at a low level.
How to make a GET request:
To get data, you must send correct headers. You need a blank line at the end to signal the end of your request.
Follow these steps:
- Set your host, port, and path variables.
- Open the connection using exec.
- Use printf to send the GET request. Use \r\n for line endings.
- Use cat or a loop to read the response.
- Close the file descriptor when finished.
Example script:
HOST="example.com" PORT=80 REQUEST_PATH="/" exec 3<>/dev/tcp/${HOST}/${PORT} printf "GET ${REQUEST_PATH} HTTP/1.0\r\nHost: ${HOST}\r\n\r\n" >&3 cat <&3 exec 3>&-
This technique helps you understand how clients and servers talk to each other. It is a useful skill for debugging on minimalist systems.