Debugging REST API with Command Line

Overview nc command

Netcat is a utility that reads and writes data across network connections, using the TCP or UDP protocols. It is designed to be a reliable "back-end" tool, used directly or driven by other programs and scripts. At the same time, it is a feature-rich network debugging and exploration tool since it can create almost any kind of connection you would need and has several interesting built-in capabilities. Common uses include:

Run man nc to have an overview.

Create Chat Server

Interestingly the nc command can be used to create chat server where multiple users can connect and send messages. The methodology used to create chat server is the same with file transfer. In this case simple text is transferred between local and remote systems. * First we will create a listening port which will be 4444 in this case.

    nc -l 4444
  • On the another terminal (on the same computer or another computer) we will connect to the chat server by providing its IP address and port number which is 4444.
    nc <ip address> 4444

where <ip address> is replaced by your local ip 127.0.0.1 if running the command on the same machine as the server, or replaced by the server ip. You will get something similar to the picture below:

Make HTTP Request

The nc command can be used to make HTTP Requests to the remote web server. Even there are more useful tools nc provides basic usage about making HTTP Requests. The HTTP Request text is redirected into the nc command where the nc command uses the specified remote web server IP address/Hostname and port number.

    printf "GET / HTTP/1.1" | nc google.com 80

Notice the pipe | commands shifts the output of the comands on left hand side to the input of the right hand side.

Alternatively the multiple lines for the HTTP Request can be expressed by using the ā€œ\r\nā€ end of line.

    printf "GET / HTTP/1.1\r\nHost:google.com\r\n\r\n" | nc google.com 80