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).
http://playground.arduino.cc/Code/WebClient
SurferTim:
I use this. Take a look at the POST method (second example on page).
Arduino Playground - WebClient
so "just" store body of http packet inside a variable, then call strlen function?
Code posted by others. YMMV
client.println("POST hxxp://yourdomain.com/SMSSite/XMLInterface/Postxml.aspx 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: ");
client.println(strlen(big_string));
// tell the server you are done with the header.
client.println();
// send the posted data.
client.print(big_string);
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: \"http://example.com/Functions/SendDataRequest\"");
client.println("User-Agent: Arduino WiFiShield");
client.println("Content-Lenght: "+body.length());
client.println("Host: arduino-data-server.appspot.com");
client.println("Connection: Keep-Alive");
client.println();
client.println(body);
client.stop();
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=\"http://schemas.xmlsoap.org/soap/envelope/\"\n\r";
res +="<S:Body>\n\r";
res +="<ns2:sendData xmlsn:ns2=\"http://example.com\">\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";
Serial.println(res);
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.println(F("Host: arduino-data-server.appspot.com"));
client.print(F("Connection: close\r\n\r\ntest=me"));
// read the server response
while(client.connected()) {
while(client.available()) {
char ch = client.read();
Serial.write(ch);
}
}
client.stop();
Serial.println(F("disconnected"));
}
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: ");
client.println(body.length());
For some reasons i still not understand
What do you think that adding (+ is not concatenate) a string and an int will do?