Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

avr39-ripe
Copy link
Contributor

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!) :)

@avr39-ripe
Copy link
Contributor Author

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..

@alonewolfx2
Copy link
Member

seems clean and helpfull for http requests. i will test it tonight.

@hreintke
Copy link
Contributor

@alonewolfx2 : @avr39-ripe :

  • Does this obsolete Access to HTTP POST body #272 completely ?
  • Has this already been tested ?
  • Can you add the sniplet to an example or provide a new example showing this functionality
  • Can you squash the commits into one ?

@avr39-ripe
Copy link
Contributor Author

@hreintke this #499 is squashed version as you requested
in addition there is example to demonstrate this and several other features and concepts, I try to make example small, simple, but reusable.
it fully obsolets #272 (but I mention again to be honest, I'm not the author of this feature, I just gather and clean things)
I test it and it works on real hardware.

@hreintke
Copy link
Contributor

Solved by #499 & #578

@hreintke hreintke closed this Jan 31, 2016
@hreintke hreintke modified the milestone: 3.0 Jan 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants