[SOVLED] AsyncWebServer doesn't receive values from HTTP_POST

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

[SOVLED] AsyncWebServer doesn't receive values from HTTP_POST

Postby GeorgeFlorian1 » Thu Feb 21, 2019 9:28 am

I've been trying to save some values from two input fields inside a HTML page.

Here is the main.cpp.
Here is the index.html.

As you can see I've been trying to save networkName and networkPassword in other const *char just to be able to Serial.print(); them but it doesn't work. They show a blank space in Serial Monitor.

Also, shouldn't request->redirect("/url"); redirect me to /url ? Because it doesn't.
I've pressed on the Sign In or Restart buttons and the Enter key on my keyboard and nothing happens.

Looking at the sketch I realize that I have no idea how to implement an action when pressing, for example, Sign In button.
How do I link those two buttons to actions in my sketch ? I haven't been able to found anything that resembles my project.

Does anybody have any idea ? Because I really don't.

Thank you !

UPDATE 1:
GeorgeFlorian1 wrote:
Thu Feb 21, 2019 2:30 pm

Sorry, I forgot to update the gist. I actually made the Sign In button into a type="submit".
After entering some values in the input fields and pressing on the Sign In button it reloads the page and gives me this error:

Code: Select all

This site can’t be reached The connection was reset.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_RESET
The Serial Monitor resets too but the output is the same. The values that I'm trying to save do not save.
Last edited by GeorgeFlorian1 on Wed Mar 13, 2019 2:02 pm, edited 4 times in total.

Denis Brion
Posts: 9
Joined: Tue Feb 19, 2019 4:45 pm

Re: Trying to save some values from two input fields from inside a HTML page

Postby Denis Brion » Thu Feb 21, 2019 10:13 am

Well, my answer wo not solve your problem, but might avoid troubles . I saw some classical troubles in your main.cpp:
line 13 : What is the use of

Code: Select all

delay(1000)
;
lines 28..34 : what happens if there are more than two lines?
I would have written (more concise; safer; easier to understand... but untested) :

Code: Select all

 
String ssid =  inputs.readStringUntil('\n');
String password = inputs.readStringUntil('\n'); 
Serial.println("ssid: " + ssid +" pwd : " + password );
inputs.close();

WiFi.begin(ssid.c_str(),password .c_str());
Again,it does not solve your issue, but might prevent fure ones...

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: Trying to save some values from two input fields from inside a HTML page

Postby GeorgeFlorian1 » Thu Feb 21, 2019 10:45 am

Denis Brion wrote:
Thu Feb 21, 2019 10:13 am
Well, my answer wo not solve your problem, but might avoid troubles . I saw some classical troubles in your main.cpp:
line 13 : What is the use of

Code: Select all

delay(1000)
;
lines 28..34 : what happens if there are more than two lines?
I would have written (more concise; safer; easier to understand... but untested) :

Code: Select all

 
String ssid =  inputs.readStringUntil('\n');
String password = inputs.readStringUntil('\n'); 
Serial.println("ssid: " + ssid +" pwd : " + password );
inputs.close();

WiFi.begin(ssid.c_str(),password .c_str());
Again,it does not solve your issue, but might prevent fure ones...
Hello ! Thank you for your reply !

line 13: delay(1000); works like a fail safe. It can happen that the serial monitor opens slightly too late after the upload and the ESP32 has already started the code execution, resulting in no output from the Serial Monitor. In this case you can add a small delay(1000); in setup() to combat it.

line 28 - 34: This sketch was made with only 2 lines written in the inputs.txt in minds. I've chosen to write that part like that just to see how and if the code works. Think of it as being in an early stage.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: Trying to save some values from two input fields from inside a HTML page

Postby boarchuz » Thu Feb 21, 2019 11:47 am

There's no submit button on your html form to actually do the action. type="submit"
For the other button, you'll probably want a little bit of javascript (eg. clear the text fields and then focus the first) but that's not important right now.
Add the submit thing and see how you get on.

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: Trying to save some values from two input fields from inside a HTML page

Postby GeorgeFlorian1 » Thu Feb 21, 2019 2:30 pm

boarchuz wrote:
Thu Feb 21, 2019 11:47 am
There's no submit button on your html form to actually do the action. type="submit"
For the other button, you'll probably want a little bit of javascript (eg. clear the text fields and then focus the first) but that's not important right now.
Add the submit thing and see how you get on.
Sorry, I forgot to update the gist. I actually made the Sign In button into a type="submit".
After entering some values in the input fields and pressing on the Sign In button it reloads the page and gives me this error:

Code: Select all

This site can’t be reached The connection was reset.
Try:

Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_RESET
The Serial Monitor resets too but the output is the same. The values that I'm trying to save do not save.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: AsyncWebServer doesn't receive values from HTTP_POST

Postby boarchuz » Fri Feb 22, 2019 12:00 am

More generally, you should learn to debug this stuff yourself. You need to use some kind of web traffic monitor to debug your form and server response. Chrome and maybe Firefox have them built in, but I like Fiddler. And you need to use Serial printouts liberally in your code to identify where things stop working the way you expect.

GeorgeFlorian1
Posts: 160
Joined: Thu Jan 31, 2019 2:32 pm

Re: AsyncWebServer doesn't receive values from HTTP_POST

Postby GeorgeFlorian1 » Wed Mar 06, 2019 3:25 pm

I fixed it.

Code: Select all

server.on("/login", HTTP_POST, [](AsyncWebServerRequest * request){
int params = request->params();
for(int i=0;i<params;i++){
  AsyncWebParameter* p = request->getParam(i);
  if(p->isFile()){ //p->isPost() is also true
    Serial.printf("FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size());
  } else if(p->isPost()){
    Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
  } else {
    Serial.printf("GET[%s]: %s\n", p->name().c_str(), p->value().c_str());
  }
} // for(int i=0;i<params;i++)
request -> send(200);
}); // server.on
That's how you obtain parameters from a HTML input field when you press on a submit button.

Who is online

Users browsing this forum: No registered users and 126 guests