HTTP POST request and Content-Lenght field

I need to send Http post request by WiFi shield, so i need to add Content-Lenght field in packet. I don't know lenght value until all packet it's write. there's a way to calculate entire lenght before sending packet?

I use this. Take a look at the POST method (second example on page).

SurferTim: I use this. Take a look at the POST method (second example on page).

so "just" store body of http packet inside a variable, then call strlen function?

Code posted by others. YMMV

client.println("POST hxxp:// HTTP/1.0");
// add a header indicating you have content-data to send.  
//If you set this too low, the server won't see it.  
//If it is too high, the server may wait, or report an error.
client.print("content-length: ");
// tell the server you are done with the header.
// send the posted data.

i paste my code, because server don’t see request because of content lenght probably wrong:

if(client.connect(server, 80)) {
    Serial.println("Connected to server, preparing request.");
    //prepare soap request
    String body = HttpRequestBody("33", "77%");
    client.println("POST /dataserver HTTP/1.1");
    client.println("Accept: text/xml, multipart/related");
    client.println("Content-Type: text/xml; charset=utf-8");
    client.println("SOAPAction: \"\"");
    client.println("User-Agent: Arduino WiFiShield");
    client.println("Content-Lenght: "+body.length());
    client.println("Connection: Keep-Alive");

String HttpRequestBody(String v1, String v2) {
  Serial.println("Generating xml message...");
  String res = "";
  res += "<?xml version=\"1.0\"\n\r";
  res +="<S:Envelope xmlns:S=\"\"\n\r";
  res +="<S:Body>\n\r";
  res +="<ns2:sendData xmlsn:ns2=\"\">\n\r";
  res +="<arg0>"+v1+"</arg0>\n\r";
  res +="<arg1>"+v2+"</arg1>\n\r";
  res +="</ns2:sendData>\n\r";
  res +="</S:Body>\n\r";
  res +="</S:Envelope>\n\r";
  return  res;
// this is spelled wrong
    client.println("Content-Lenght: "+body.length());
// it should be this
    client.println("Content-Length: "+body.length());

So, i've fix that error, but problem still exist. I don't think it's a server problem, because, if i send GET request, on server's log i see request. But i need to send POST XD I can't understand why, because the content of HTTP request on my code, it's a cut and paste of HTTP POST request sniffed with wireshark by using a web client for connect to server

Try something simple first. See if it shows up in the log. This also displays the server response. That might help you troubleshoot this.

if(client.connect(server, 80)) {
    Serial.println(F("Connected to server, preparing request."));
    client.println(F("POST /dataserver HTTP/1.1"));
    client.println(F("Content-Type: application/x-www-form-urlencoded"));
    client.println(F("Content-Length: 7"));
    client.print(F("Connection: close\r\n\r\ntest=me"));

    // read the server response
    while(client.connected()) {
        while(client.available()) {
            char ch =;

So, finally i've find the bug. I explain you. For some reasons i still not understand, the command:

client.println("Content-Length: "+body.length());

was wrong. In fact server couldn't interpret that line, and discard my request. The right command (tested) was

client.print(("Content-Length: ");

For some reasons i still not understand

What do you think that adding (+ is not concatenate) a string and an int will do?