macchina-io / macchina.io

macchina.io EDGE is a powerful C++ and JavaScript SDK for edge devices, multi-service IoT gateways and connected embedded systems.
https://macchina.io
GNU General Public License v3.0
512 stars 152 forks source link

POST json data from webUI to macchina device #107

Closed zaleksa closed 2 years ago

zaleksa commented 3 years ago

Hi,

I am trying to send json data from web UI written in AngularJS to backend - bundle running on macchina device, using HTTP POST request:

this.httpPostControlData = function(jsondata, onsuccess, onfailure){ $http({ method: "POST", url: getControlDataURL, headers: { "Content-Type":"application/json" }, data: jsondata }).success(function(data){ if(!data.error) onsuccess(data) else onfailure(data.error); }).error(function(){ onfailure("Server request failed."); }); };

URL getControlDataURL is pointing to getControlData.jss file which has:

if(request.method == "POST"){ console.log(JSON.stringify(request)); myBackendBundle.instance().updateControlData(request.content); }

In console it can be seen that buffer and content are empty:

2021-06-14 08:44:36.514 [Information] osp.bundle.io.macchina.webui.simplhub<30>: {"credentials":null,"cookies":{"osp.web.session.macchina.io":"5bad11ea039278d3e951c55d4b44fb4a4db095ba","XSRF-TOKEN":"7d56f1e64d77abc22f1cf97838312862c32e73c0"},"timeout":30,"buffer":"","content":"","contentType":"application/json","version":"HTTP/1.1","uri":"/macchina/simplhub/getControlData.jss","method":"POST"}

In web browser's Inspect/Network window it can be seen that request is filled with data (Content-Length: 861, Request Payload with json data):

Request

I read article https://macchina.io/docs/00200-MacchinaJSProgramming.html and saw example how to send data in opposite way, from backend to webUI, using POST HTTP request and content for storing data (Section: HTTPRequest and HTTPResponse Objects).

Where am I wrong with this, or maybe it is not supported at all?

p.s. Sending data is working with HTTP GET request and data stored as params in URI, but I want to send data within request as content data.

Thanks in advance,

Aleksandar

zaleksa commented 2 years ago

Eventually I switched from JSS to C ++ request handler which can stream request as JSON object:

void RequestHandler::handlePostData(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
{
    try
    {
        Poco::AutoPtr<Poco::Util::JSONConfiguration> pJSON = new Poco::Util::JSONConfiguration;
        pJSON->load(request.stream());

        response.setContentType("application/json");
        response.setChunkedTransferEncoding(true);

        if(pJSON->has("some_key"))
        {
            // Do something

            response.send()
                << "{"
                <<   "\"some_key\":" << "\"value\"" // Optional
                << "}";
        }
        else
        {
            response.send()
                << "{"
                << "}";
        }
    }
    catch (Poco::Exception& exc)
    {
        _pContext->logger().log(exc);

        response.setContentType("application/json");
        response.setChunkedTransferEncoding(true);
        response.send()
            << "{"
            <<   "\"error\":" << Utility::jsonize(exc.displayText())
            << "}";
        return;
    }
}

Regards Aleksandar