http.POST to google sheets via WebApp works but gives RC 400 anyways

Hallo everybody,

i´ve made an webApp with google Script to write a log to google Sheets.

So far so good, it even works, but still i get RC400 from that call.

I know i could do it with http.Get as well, but I prefer using Post method.

Here is what I did so far:

function doPost(e) {
  // this is google script for logging
  
  var result = { success: false, message: '' };
  try {
    var spreadsheet_id = '122KzdHs12345678909874563214EmiIoiQfNwreX774';
    var spreadsheetname = 'Log';
    var sheet = SpreadsheetApp.openById(spreadsheet_id).getSheetByName(spreadsheetname);

    if (!sheet) {
      result.message = 'Sheet nicht gefunden.';
      return ContentService.createTextOutput(JSON.stringify(result))
        .setMimeType(ContentService.MimeType.JSON);
    }

    var date = e.parameter.date ? String(e.parameter.date) : null;
    var time = e.parameter.time ? String(e.parameter.time) : null;
    var dongle_id = e.parameter.dongle_id ? String(e.parameter.dongle_id) : null;


    sheet.appendRow([new Date(), date, time, dongle_id]);
    result.success = true;
    result.message = 'Eintrag erfolgreich hinzugefügt.';
  } catch (error) {
    result.message = 'Fehler beim Hinzufügen des Eintrags: ' + error.toString();
  }

  return ContentService.createTextOutput(JSON.stringify(result))
    .setMimeType(ContentService.MimeType.JSON);
}

And this my Arduino Code:

// URL Web-App
const char* webAppUrl = "https://script.google.com/macros/s/AKfycbxvd901234567890123456789gNn8iU06v4R5gHVPHJ7pFhasQRLUWJRb8-vRVniNJB-Q/exec";


void postlog() {
  HTTPClient http;
  http.begin(webAppUrl); // Start Connection
  http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);

  http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // Header hinzufügen

  String date = "test"; //"02.08.2019";
  String time = "test2";  // "11:56:24";
  String dongle_id = "Test3" ; //"00001001010010011110110001";

  // Erstellen der POST-Daten
  String postData = "date=" + date + "&time=" + time + "&dongle_id=" + dongle_id;

  // Senden der POST-Anfrage
  int httpResponseCode = http.POST(postData);

  // Check answer
  String response = http.getString(); // Antwort als String
  Serial.println("HTTP Response code: " + String(httpResponseCode));
  Serial.println("Response: " + response);

  http.end(); // Beenden der Verbindung
}

And this is what happens:

I see a new line always when i run this function, perfectly as I want.

But I see in Serial Monitor:

20:03:47.518 -> HTTP Response code: 400
20:03:47.518 -> Response: <!DOCTYPE html>
20:03:47.518 -> <html lang=en>
20:03:47.518 ->   <meta charset=utf-8>
20:03:47.518 ->   <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
20:03:47.518 ->   <title>Error 400 (Bad Request)!!1</title>
20:03:47.518 ->   <style>
20:03:47.518 ->     *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
20:03:47.518 ->   </style>
20:03:47.518 ->   <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
20:03:47.518 ->   <p><b>400.</b> <ins>That’s an error.</ins>
20:03:47.518 ->   <p>Your client has issued a malformed or illegal request.  <ins>That’s all we know.</ins>

I´ve tried testing via postman and there it works perfectly.

While writing this post I checked again in Postman, but this time i had a look into the console where all the request and answers logged completely.

In fact it is that a do.Post request in my case gets redirect to a do.Get Url.

This doesn´t seem to be very uncommon and when searching for this in the Web you find a bunch of further informations.

For starters I reccomend this

For me this is the solution (even if i don´t understand why i get rc400 and not 302 or else...) and now I´ll think about sending a get directly (means changing also the google WebApp / Script) or if i will implement the redirect myself on arduino side.

I hope this will help also others facing the same problem.

cheers Richarde

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.