Listening on specific port number using netcat to receive http POST messages

The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet(1) does with some.

Common uses include:
· simple TCP proxies
· shell-script based HTTP clients and servers
· network daemon testing
· a SOCKS or HTTP ProxyCommand for ssh(1)
· and much, much more

Ref – from man page of nc

Now, here we will see how to debug whether we are receiving HTTP POST messages on specific port, if we are trying to develop some application which indents to send some HTTP POST messages and we want to see what the server is receiving at its end.

For this usecase, we will try to listen on one socket, and then from another terminal we will try to sent HTTP POST message to the port number as we used in nc as,

1. open terminal and type command,

 nc -l 8080 

Here, 8080 is our test port, you can use any port or the port number which you are using in your application.

Now open another terminal, and type following command as, [ NOTE : This is just our test application/bash script/command, you may run your exact application which you are debugging to understand what it is sending to the server ]

curl -k \
        -H "Content-Type:text/xml; charset=UTF-8" \
        -X POST \
        -d '<?xml version="1.0" encoding="UTF-8"?>
                <address>
                  <name>Myname</name>
                  <apartment>Myapartment</apartment>
                  <city>Mycity</city>
                  <pincode>12345678</pincode>
        </address>' http://localhost:8080

Here, since we have started nc command on same PC, we are using localhost, if you have nc command started on another PC on which you want to see what you receive on that PC’s port, you need to use IP address as, http://IP_ADDRESS:PORT_NUMBER

Now, with this you can see, exactly what you are receiving at the first terminal where we started netcat,

$ nc -l 8080 
POST / HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.47.0
Accept: */*
Content-Type:text/xml; charset=UTF-8
Content-Length: 181

<?xml version="1.0" encoding="UTF-8"?>
		<address>
		  <name>Myname</name>
		  <apartment>Myapartment</apartment>
		  <city>Mycity</city>
		  <pincode>12345678</pincode>
	</address>

So, this nc command listening on port 8080 showed that port received at HTTP POST message with xml as Content Type and also exact xml which client/app sent to server.

Leave a Reply

Your email address will not be published. Required fields are marked *