Skip to content

POST Request from web page #3211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue May 7, 2017 · 10 comments
Closed

POST Request from web page #3211

ghost opened this issue May 7, 2017 · 10 comments

Comments

@ghost
Copy link

ghost commented May 7, 2017

Hello everyone!
Web-Server esp8266 is work on this core.
I am send the POST requests /dev.sc-write from the web page (esp8266)
How can I take useful information from a query? (Sending Not a File)

@horendus
Copy link

horendus commented May 8, 2017

You need to attach a POST method handler to the directory of where you are submitting the POST request.

server.on(/, "POST", processPostData()); <---This will handle post requests submitted to / or "root")

The POST handling method should then iterate through the POST requests arguments and be processed in what ever way you see fit. The names and values of the POST requests elements can be accessed with server.argName() and server.arg(). The total number of post argument that have been received is available through server.args() (a return value is given)

(Note the bellow example I have written is untested and uncomplied)

void processPostData(){
 if (server.args() > 0){
    for (int i = 0; i < server.args();i++;){
        Serial.print("POST Arguments: " ); Serial.println(server.args(i));
        Serial.print("Name: "); Serial.println(server.argName(i));
        Serial.print("Value: "); Serial.println(server.arg(i));
   }
}

Hope this helps.

@ghost
Copy link
Author

ghost commented May 8, 2017

Thank you!
It work:

HTTP.on("/dev.sc-write", HTTP_POST, DevSCWrite);

void DevSCWrite() {
Serial.println(HTTP.arg("sc")); // For this key we take data
HTTP.send(200, "text/plain", "OK");
}

There is a problem in the source library (file Parsing.cpp) is case-sensitive.
For proper operation, it is necessary to transmit the POST headers as follows:
Content-Type instead content-type!!!

@igrr
Copy link
Member

igrr commented May 8, 2017

How are you sending the POST request? Could you post a code/webpage snippet?

@ghost
Copy link
Author

ghost commented May 8, 2017

I do this on nodejs + preact

postSend() {
    const data = 'Test Data: bla-bla';
    var body = new FormData;
    body.append('sc', data);
    fetch('dev.sc-write', {
        method: 'POST',
        body
    });
}

@ghost
Copy link
Author

ghost commented May 8, 2017

Are you planning to remove this bug from the library Parsing.cpp and make case-insensitive headers?

@igrr
Copy link
Member

igrr commented May 8, 2017

I think case-insensitive parsing was implemented in this PR: #2131

You can try using the master branch to see if it resolves the issue.

@ghost
Copy link
Author

ghost commented May 8, 2017

Thank you!
I did so, but I would like everything to work correctly from the package, without editing the author's library :)
Why do not you want to add a few lines of code to the source?

@igrr
Copy link
Member

igrr commented May 8, 2017

Why do not you want to add a few lines of code to the source?

Could you clarify what you mean? The fix for the issue has already been merged into the master branch. When the next stable package is released, the fix is going to be included.

@ghost
Copy link
Author

ghost commented May 8, 2017

I meant all the HTTP headers, not just the Content-Type.

@igrr
Copy link
Member

igrr commented May 8, 2017

Yes the PR mentioned above fixed parsing of other headers as well, not just the Content-Type:

4897e00

@igrr igrr closed this as completed May 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants