Integralist Integralist - 15 days ago 5
HTTP Question

simple single HTTP response using netcat as web server on macOS

I'm using macOS and have netcat installed via Homebrew

$ brew info netcat
netcat: stable 0.7.1 (bottled)
Utility for managing network connections
http://netcat.sourceforge.net/
/usr/local/Cellar/netcat/0.7.1 (11 files, 104.2K) *
Poured from bottle on 2016-06-21 at 12:19:18
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/netcat.rb


I'm just trying to get netcat to respond to an incoming curl request, but I think the problem I'm having is that netcat doesn't know when to respond.

Here is the content expected to be sent back, it's stored in
response.txt
:

HTTP/1.1 200 OK


Here is the netcat command being run in one terminal shell:

sudo nc -l -p 80 < response.txt


Here is the curl command being run in another terminal shell:

curl 127.0.0.1:80


Could someone help me understand what I would need to do in order to get this working as intended.

I can get this to work by using netcat instead of curl for the connection, but ideally I'd like a non-netcat client (such as curl or a web browser) to make a connection to localhost:80 and have the first instance of netcat respond.

Thanks.

Answer

Try using the macOS netcat instead of homebrew, and drop the -p option:

sudo /usr/bin/nc -l 80 < response.txt