1

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);
}
2
  • 2
    remove \r\n. you have println. move client.println(); before client.println(content);
    – Juraj
    Jul 14, 2023 at 17:00
  • @Juraj I solved a couple issues... this was one of them. Will post solution soon. Thanks for your help :)
    – NRav
    Jul 14, 2023 at 19:55

1 Answer 1

0

There were a couple issues that I finally managed to solve... I was able to get it to work by not using ArduinoHTTPClient. Here is working code for anyone else who stumbles here.

TinyGsmClient client(modem);

void fileUpload(String filename){
  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--\r\n";
  //Serial.println(content);
  client.println(String("POST ") + "/upload.php" +" HTTP/1.1"); // or wherever you need to call/print to.
  client.println(String("Host: ") + server);
  client.println("Content-Length: "+String(content.length()+dataFile.size()+closingContent.length()));
  client.println("Content-Type: multipart/form-data; boundary=boundary1");
  client.println();
  client.println(content);
  if (dataFile) {  // start sending file content
    String data = ""; 
    while (dataFile.available()) {
      char c = dataFile.read();
      data += c;
      //write every line break...
      if(c == '\n'){
        client.write(data.c_str());
        data = "";
      }
    }     
    dataFile.close();
  }
  client.print(closingContent);
  unsigned long timeout = millis();
  while (client.connected() && millis() - timeout < 50000L) {
    // Print available data
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
      timeout = millis();
    }
  }
  client.stop();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.