diff options
author | Fulup Ar Foll <fulup@iot.bzh> | 2016-05-13 19:10:32 +0200 |
---|---|---|
committer | Fulup Ar Foll <fulup@iot.bzh> | 2016-05-13 19:10:32 +0200 |
commit | 6f058dbce7f7f96c458820534f774b9b2b358607 (patch) | |
tree | 7ea56e7d85ffaea4c3482d593bad5d117ee43e20 | |
parent | b5cf93aed93e7f331eb645c8afe5317fb67ee50e (diff) |
Fixed Sample POST/JSON
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | src/afb-hsrv.c | 4 | ||||
-rw-r--r-- | test/sample-post.html | 62 |
3 files changed, 64 insertions, 9 deletions
@@ -45,6 +45,13 @@ $ cmake ..<br /> $ make; make install<br /> ``` +### Testing/Debug +``` +$ AFB_DAEMON_DIR=$HOME/afb-daemon +$ $AFB_DAEMON_DIR/build/afb-daemon --help +$ $AFB_DAEMON_DIR/build/afb-daemon --port=1234 --token='' --ldpaths=$AFB_DAEMON_DIR/build --sessiondir=/tmp --rootdir=$AFB_DAEMON_DIR/test +``` + ### Starting ``` $ afb-daemon --help diff --git a/src/afb-hsrv.c b/src/afb-hsrv.c index d2356484..5b0153c2 100644 --- a/src/afb-hsrv.c +++ b/src/afb-hsrv.c @@ -156,7 +156,9 @@ static int access_handler( if (hreq->postform == NULL) afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR); return MHD_YES; - } else if (strcasestr(type, JSON_CONTENT) == NULL) { + } else if (strcasestr(type, JSON_CONTENT) != NULL) { + return MHD_YES; + } else { afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE); return MHD_YES; } diff --git a/test/sample-post.html b/test/sample-post.html index 337df97b..b1486c3b 100644 --- a/test/sample-post.html +++ b/test/sample-post.html @@ -3,13 +3,59 @@ <title>Sample Post test</title> <body> <h1>Sample Post test</h1> + + <h2>Sample Post File</h2> <form enctype="multipart/form-data"> - <input type="file" name="file" /> - <input type="hidden" name="hidden" value="bollobollo" /> - <ol> - <li><button formmethod="POST" formaction="api/post/ping">ping</button> - <li><button formmethod="POST" formaction="api/post/upload-json">upload json</button> - <li><button formmethod="POST" formaction="api/post/upload-image">upload image</button> - <li><button formmethod="POST" formaction="api/post/upload-music">upload music</button> - <li><button formmethod="POST" formaction="api/post/upload-appli">upload application</button> + <input type="file" name="select input file" /> + <input type="hidden" name="hidden" value="bollobollo" /> + <br> + <button formmethod="POST" formaction="api/post/upload-image">Post File</button> </form> + + <h2>Sample Post JSON</h2> + + <form id="jsonform"> + <input name='name' value='MyName'> + <input name='info' value='MyInfo'> + <select name='option'> + <option selected>opt1</option> + <option>opt2</option> + <option>opt3</option> + </select> + <label>ticked</label> + <input type='checkbox' name='ticked'> + </form> + <p><input id="jsonrep" placeholder="AFB-daemon Response" readonly style="width: 100%"> + <button onclick="xpost();">Post JSON</button> + + <script> + // post bouton press post form as JSON + var xpost=function() { + var jform={}; + var xform = document.querySelector('#jsonform').elements; + var xreqt = new XMLHttpRequest(); + xreqt.open("POST", "api/post/upload-json", true); + xreqt.setRequestHeader('Content-Type', 'application/json'); + + // Serialise jform in JSON + for(var ind = 0; ind < xform.length; ind++) { + console.log ("name=%s value=%s", xform[ind].name, xform[ind].value); + jform[xform[ind].name] = xform[ind].value; + } + + // display afb-daemon return values + xreqt.onload = function () { + var result = "Status:" + xreqt.status + " Value:" + xreqt.responseText; + document.getElementById("jsonrep").value = result; + console.log (result); + }; + + // Post Form as JSON + console.log ("Posting jform=%j", jform); + xreqt.send(JSON.stringify(jform)); + }; + </script> + + + </body> +</html>
\ No newline at end of file |