I have been having trouble trying to send a small txt file to my server using the following function with TinyGSM. I know I am connected to the internet and reaching the web server, however, I keep receiving a 400 Bad Request
code. I think something is wrong with my PostHeader
, but I have no clue where or what it could be... I know I am connecting to my server as I try a default get and return webpage text...
TinyGsm modem(SerialAT);
TinyGsmClient client2(modem);
HttpClient client(client2, server, port);
void fileUpload(String filename){
if(!initSD()){
return;
}
File dataFile = SD.open(filename.c_str());
Serial.println("Uploading File...");
if (!client.connect(server, port)) {
Serial.println("Failed connecting to Server");
return;
}
String fileType = "text/plain";
String content = "--boundary1\r\n";
content += "Content-Disposition: form-data; name=\"fileToUpload\"; filename="+String(filename)+"\r\n"; // the fileToUpload is the form parameter
content += "Content-Type: "+fileType+"\r\n\r\n";
//after this, post the file data.
String closingContent = "\r\n--boundary1--";
//Serial.println(content);
client.println(String("POST ") + "/upload.php" +" HTTP/1.1\r\n");
client.println(String("Host: ") + server + "\r\n");
client.println("Content-Type: multipart/form-data; boundary=boundary1\r\n");
client.println("Connection: close\r\n");
client.println("Content-Length: "+String(content.length()+dataFile.size()+closingContent.length())+"\r\n");
client.println(content);
client.println();
if (dataFile) { // start sending file content
while (dataFile.available()) {
char c = dataFile.read();
client.print(c);
}
dataFile.close();
}
client.println(closingContent);
Serial.println("File Sent.");
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
}
client.println();
beforeclient.println(content);