NodeMCU 301 error but URL is valid?

I'm using a NodeMCU as a data logger, or at least trying to. When I try to upload data using a URL (with variables in the URL) I get a 301 error.

The error includes the correct URL which is identical to the one used. I output the URL used to serial, and if I copy it into a browser it works.

I feel like I'm missing something simple but I just can't see it. It's not https.

Here's the relevant bit of code (I've removed my domain):

void uploadValues() {
  Serial.println("Uploading data to server...");
  WiFiClient client;
  HTTPClient http;    //Declare object of class HTTPClient

  String postData;

  byte rain = map(analogRead(A0), 1024, 0, 0, 100);

  //Post Data
  postData = "http://  -={ MY DOMAIN }=-  /shed/up.php?BMEtemp='" + String(bme.readTemperature(), 2);
  postData += "'&humidity='" + String(bme.readHumidity(), 2);
  postData += "'&INTtemp='" + String(sensors.getTempCByIndex(0), 2);
  postData += "'&EXTtemp='" + String(sensors.getTempCByIndex(1), 2);
  postData += "'&LASERtemp='0'";
  postData += "&pressure='" + String(bme.readPressure() / 100.0F, 2);
  postData += "'&RAINstatus='";
  if (digitalRead(D6) == LOW) {
    postData += "1";
  } else {
    postData += "0";
  }
  postData += "'&RAINvalue='" + String(rain) + "'";

  http.begin(client, postData);
  int httpResponseCode = http.GET();
      
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    String payload = http.getString();
    Serial.println(payload);
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }

  http.end();  //Close connection

  Serial.println("===================");
  Serial.println(postData);
  Serial.println("===================");
  Serial.println(" ");
}

You can try to isolate the error by using a fixed string for postdata. For example
postData = "http://192.168.1.1/shed/up.php?BMEtemp=10&humidity=10&INTtemp=10&EXTtemp=20&LASERtemp=0&pressure=0&RAINstatus=1&RAINvalue=0";

Thanks for the suggestion. It still doesn't work if I use a static string like that.

I spotted that I was using '' in the URL rather than in the PHP which receives the data so I've fixed that, but I'm still getting a 301. I've stripped it back to just the domain and it's still a 301.

Ok so I've done some testing and it looks like a quirk of dreamhost.

If I load a URL from any of my domains on http or https I get a 301 redirecting me to exactly the same URL. If I load a URL from another domain it works fine.

Guess I'll have to set up a server at home.

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