Home » Linux, OS Concepts and Networking » Linux Networking » Send data using http POST from client and receive response from server using netcat over specific port

Send data using http POST from client and receive response from server using netcat over specific port

We create a server using netcat command which listens on 8909 port as below, This server responds with HTTP OK once it receives a request from client. Along with sending HTTP OK, this server also executes get-cmdline.sh script to get the Linux boot command line by reading proc filesystem file /proc/cmdline and sends it to the client.

$ vim nc_response.sh
#!/bin/bash
while true;
        do {
                echo -e 'HTTP/1.1 200 OK\r\n'; sh get-cmdline.sh;
        } | nc -l 8909;
         done
$ vim get-cmdline.sh
#!/bin/bash
echo "This is response from nc with http OK"
echo "\n"
echo "Cmdline is :"
cat /proc/cmdline
echo "\n"

Now, start listening on socket as,

$ bash nc_response.sh

Now, on client/application side, lets write a curl POST as,

$ vim curl-xml-post.sh
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:8909 -v

Now, start the client as,

$ bash curl-xml-post.sh

And you will get the response as,

$ bash curl-xml.sh 
Note: Unnecessary use of -X or --request, POST is already inferred.
* Rebuilt URL to: http://localhost:8909/
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8909 (#0)
> POST / HTTP/1.1
> Host: localhost:8909
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type:text/xml; charset=UTF-8
> Content-Length: 181
> 
* upload completely sent off: 181 out of 181 bytes
> HTTP/1.1 200 OK
* no chunk, no close, no size. Assume close to signal end
< 
This is response from nc with http OK
 
 
Cmdline is :
BOOT_IMAGE=/boot/vmlinuz-4.4.0-71-generic root=UUID=6dc72f0f-0d95-4e80-8ab7-c2331c79ca08 ro quiet splash vt.handoff=7
 
 
* Closing connection 0

ESP32 Asynchronous Web Server using...
ESP32 Asynchronous Web Server using Arduino IDE

1 thought on “Send data using http POST from client and receive response from server using netcat over specific port”

Leave a Comment

Sign up to our newsletter!