-
-
Notifications
You must be signed in to change notification settings - Fork 345
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
Add ability to get raw http request body #487
Conversation
sniplet for using above feature in webserver handling code for some url: if (request.getBody() == NULL)
Serial.println("NULL bodyBuf");
else
{
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(request.getBody());
root.prettyPrintTo(Serial);
if (root["start_minutes"].success()) // Settings
{
ActiveConfig.start_minutes = root["start_minutes"];
ActiveConfig.stop_minutes = root["stop_minutes"];
ActiveConfig.cycle_duration = root["cycle_duration"];
ActiveConfig.cycle_interval = root["cycle_interval"];
}
}
saveConfig(ActiveConfig); in submit form event handling we can send form in json as function post_cfg(event) {
event.preventDefault();
var formData = {
'start_minutes' : document.getElementById('start_minutes').value,
'stop_minutes' : document.getElementById('stop_minutes').value,
'cycle_duration' : document.getElementById('cycle_duration').value,
'cycle_interval' : document.getElementById('cycle_interval').value
};
$.ajax({
type : 'POST',
url : '/config',
contentType : 'application/json; charset=utf-8',
data : JSON.stringify(formData),
dataType : 'json'
})
} you may ask WHAT is the proffit of whole this stuff instead of simply use HTML FORMS? - you can as I said validate values more deeply then even html5 inputs allow, you can pre-proccess form data and send result, you can send more complex data from page to server, you can talk with some REST server (may be one made from esp8266) in more native, json manner and so on.. |
seems clean and helpfull for http requests. i will test it tonight. |
|
@hreintke this #499 is squashed version as you requested |
It is really cleaned version of PR #272 by @dereulenspiegel. I just polish it and remove unwanted formatting. It intendet to return raw http request body for further parsing, for example by ArduinoJson library. I think that POST-ing json from web is much more efficient and can send more complex and structured data than formURLencoded content-type. Moreover, by adding just little JS-code on frontend (web-page) we can make some data validation/modification/processing before sending to esp8266 web-server and off-load our little buddy on client (more powerful in fact!) :)