POST application/x-www-form-urlencoded?

Using a microcontroller (ESP8266) I am trying to send data by POST, “application/x-www-form-urlencoded” data.

I have been able to send data using cURL; but, unsuccessful using POST method with “application/x-www-form-urlencoded” data.

What are the POST requirements for: “observation-weather.000webhostapp.com”?

William

Hi, there are no extra requirements, however POST, GET, and cURL have been having problems lately and fixing it is hard since we don’t know what the problem is. Keep trying and hopefully it will be fixed soon.

1 Like

cURL for me is working; data gets POSTED after being recevived by “dataCollector.php.”

if (client.connect(server, 80))     
{

	Serial.println("connected to server");
	// if you get a connection, report back via serial:
	WiFi.printDiag(Serial);

	client.println("POST  /BME280/dataCollector.php HTTP/1.1"); //change this if using your Sub-domain
	client.print("Host: https://observation-weather.000webhostapp.com");  //change this if using your Domain
	client.println("User-Agent: ESP8266/1.0");
	client.println("Connection: close"); 
	client.println("Content-Type: application/x-www-form-urlencoded");
	client.print("Content-Length: ");
	client.print(data.length());
	client.print(data);
	client.print("\r\n");
	client.print("\n");
	client.flush();
	client.stop(); 

	Serial.println("\n");	
	Serial.println("My data string im POSTing looks like this: ");
	Serial.println(data);
	Serial.println("And it is this many bytes: ");
	Serial.println(data.length());
            Serial.println("\n");		
	delay(2000);

 }

Is port 80 the problem?

William

Nice to hear that. I think it is a temporary error right?

Question about default security configuration on “observation-weather.000webhostapp.com.”. Can website accept anonymous data from POST method to php? Is hotlink enabled? Hotlink needs to disabled; I have been advised from author of php code, of these two php code requirements.

Included a snippet of code that I am trying to use; earlier in this message thread.

What is the best practice for using POST method of uploading data to php?

William

1 Like

I didn’t really got your question.

As I can understand, you need to know about POST method.

There are some different methods to interact between the client and the server with HTTP. Nowadays, all the methods are deprecated except POST and GET.

  • GET - Requests data from a specified resource
  • POST - Submits data to be processed to a specified resource

If you are posting sensitive data use POST and never use GET method.

How are you going to upload data? As I can see you aren’t using a browser (js). Can you describe your project, programming languages used in client-side and other information.

Thanks

1 Like

Project uses a ESP8266 development board and a BME280 sensor to measure temperature, humidity, and barometric pressure all in one sensor. Language used for project is Arduino IDE, C++. Sensor readings are assembled into String format. Each set of data has a name and a value. I am able to send the String using cURL; still have had no joy send from the Arduino sketch that is doing the sensor readings.

How String is “assembled” for POST method:

    char fahren[9];// Buffer big enough for 9-character float
dtostrf(fahrenheit, 6, 2, fahren); // Leave room for too large numbers!

char heat[9]; // Buffer big enough for 9-character float
dtostrf(heat_index, 6, 2, heat); // Leave room for too large numbers!

char hum[9]; // Buffer big enough for 9-character float
dtostrf(bme_humidity, 6, 2, hum); // Leave room for too large numbers!

char dewpt[9]; // Buffer big enough for 9-character float
dtostrf(dew, 6, 2, dewpt); // Leave room for too large numbers!

char cpressure[9]; // Buffer big enough for 9-character float
dtostrf(currentPressure, 6, 2, cpressure); // Leave room for too large numbers!

char bars[9]; // Buffer big enough for 9-character float
dtostrf(millibars, 6, 2, bars); // Leave room for too large numbers!

char altit[9]; // Buffer big enough for 9-character float
dtostrf(bme_altitude, 6, 2, altit); // Leave room for too large numbers!


 data = "fahren="  
          
                                 +  (String)fahren
    
+ "&heat="                +  (String)heat
     
+ "hum="                  +  (String)hum

+ "&dewpt="             +  (String)dewpt
      
+ "&cpressure="       +  (String)cpressure
      
+ "bars="                  +  (String)bars
      
+ "&altit="                 +  (String)altit; 

cURL (with “fake data”) -> “dataCollector.php”-> “dataDisplayer.html” Produces “dataDisplayer.html” page with appended data.

Arduino sketch -> POST method -> “dataCollector.php”-> “dataDisplayer.html” Does not produce appended data on “dataDisplayer.html” page.

William

Made change to my Arduio/ESP8266 code; now using ESP8266HTTPClient library to POST String data.

Here is snippet:of the code:

     data = "fahren="     
                                     +  fahren
        
    + "&heat="                +  heat
         
    + "&hum="                 +  hum
    
    + "&dewpt="               +  dewpt
          
    + "&cpressure="         +  cpressure
          
    + "&bars="                 +  bars
          
    + "&altit="                  +  altit;
    
    
    
    //Serial.print(data);
    
   
  if(WiFi.status()== WL_CONNECTED)
  {   //Check WiFi connection status

    HTTPClient http;    //Declare object of class HTTPClient

    http.begin("http://observation-weather.000webhostapp.com/BME280/dataCollector.php");      //Specify request destination
    http.addHeader("Content-Type", "application/x-www-form-urlencoded");  //Specify content-type header

    int httpCode = http.POST(data);   //Send the request
    String payload = http.getString();                  //Get the response payload

    Serial.println(httpCode);   //Print HTTP return code
    Serial.println(payload);    //Print request response payload

    http.end();  //Close connection

  }
  else
  {

    Serial.println("Error in WiFi connection");   

  }

  delay(30000);  //Send a request every 30 seconds

Now successful from Arduino sketch -> HttpClient POSI -> “dataCollector.php”-> “dataDisplayer.html” Does produces appended data on “dataDisplayer.html” page.

Project web page

“Observations” are updated every fifth-teen minutes; so are the Graphs. Time interval uses Network Time Protocol; no Real Time Clock is required.

William

Why not try the GET method?

What would I gain using GET; since it is already working with POST?

William