Help needed with a simple POST-Request

Hi,

I need some help with a really basic POST-Request. I´m using the following code (based on some examples):

TCPClient client;
byte server[] = { 192, 168, 178, 28 };

void setup() {
  Serial.begin(9600);
  while(!Serial.available());
  
  delay(500);
  
  Serial.println("go, go, go...");

  if (client.connect(server, 1400)) {
    Serial.println("connected...");

    client.println("POST /DoATest HTTP/1.0");
    client.println("test"); //does not work
    client.println();

    delay(250);
    
    Serial.println("request sent...");
  }
  else {
    Serial.println("connection failed");
  }
}

void loop() {
  if (client.available()) {
    Serial.print( (char) client.read() );
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    delay(1000);
  }
}

Now here is the problem: I´m not able to receive the message-body (“test”).
I get the first line with a simple server (see last part) and the headers, but the body will be lost.

If I change the lines to (according to some example for arduino and this forum)

client.println("POST /DoATest HTTP/1.0");
client.println();
client.println("test"); //does not work

the request will never be sent.

After hours of testing and changing I had one version that works correct, but after some more changes it didn´t work again and the old version was lost. What am I doing wrong?
For my project I need to send xml data in the body. But I am far away, if I cannot send ‘test’…

Simple nodeJS-Server to check and monitor POST-Requests:

var http = require('http');

http.createServer(function (req, res) {
  console.log("\n\n");
  console.log(req.method + " " + req.url + " " + req.httpVersion);
  console.log(req.headers);

  if(req.method == 'POST') {
    req.on('data', function(chunk) {
      console.log(chunk.toString());
    });
  }

  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1400);

console.log('Server running...');

Thanks for your help…

Dang. i wished i bookmarked the TCP example.

@bko has a sample code for TCPclient and it’s working well to get started.

Just give him some time before he sees this :smiley:

1 Like

Hi @Sandy

You might be very interested in this library that another user put together. People seem to have good luck with it.

Your post request is not formatted the way most are, but since you have your own node-JS code reading it, I am not sure that matters. For a real http server, you do need a content length header. If you want to write your own, you can use that library above for a reference.

1 Like

You may be able to drop your delay(250) down a little. I’ve had success as low as delay(10), but below that gets somewhat sketchy depending on network latency.

Definitely try adding the Content-Length header. Your mileage will vary from server-to-server, but most require it for HTTP POST requests.

Thanks for the answers. I will definitely try to implement them.
Btw. does anyone have a good class to analyse an xml response?
I know there is a good one for JSON, but I can´t find one for xml.