Skip to content
Tech News
← Back to articles

TIL: You can make HTTP requests without curl using Bash /dev/TCP

read original more articles
Why This Matters

This article highlights a powerful yet simple method for making HTTP requests directly from Bash without relying on external tools like curl or wget. By leveraging Bash's internal /dev/tcp feature, developers can perform network checks and communicate with services in minimal environments, such as stripped-down containers, enhancing automation and scripting flexibility. This technique is particularly valuable for troubleshooting, scripting, and environments with limited toolsets.

Key Takeaways

I needed to check that one container could reach another over an internal Docker network: a plain GET /health against a service on a shared network. The obvious move is curl http://service:8642/health . But this app image was stripped right down, with no curl or wget and nothing else around that I could use to open a socket.

As it turns out, bash can speak HTTP by itself bash can open a TCP socket, and you can write a small HTTP request to it by hand. Opening a connection to a host and port and writing the request needs nothing beyond the shell that’s already there:

bash Copy exec 3<>/dev/tcp/service/8642 printf 'GET /health HTTP/1.1\r

Host: service\r

Connection: close\r

\r

' > & 3 cat < & 3

service here is just the hostname of whatever you’re talking to. It has to resolve and be reachable from wherever you run this, so it needs to be set up first: a container or service name on a Docker network you’ve configured, or any DNS name that resolves. Swap in your own host and port.

That prints the whole response: the status line, the headers, the blank line, and the body. To add a header, such as an Authorization: Bearer token, put another \r

-terminated line before the blank line that ends the request:

... continue reading