Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

c++ - POST to return results in the same request as response body (cpp REST POST SDK)

To explain the context of my problem. My application interface to a Manufacturing execution systems (MES) via a Flask server. I use Postman for debugging my work.

I have an example that listen to the MES, when the MES sends (POST) my application an order, I successfully captures it, parses the JSON to my own data structure. My own algorithm to process this data structure and the results need to be return to the MES as JSON.

When I use two parts, the MES send a POST and I capture and return the results when the MES send a GET, it works fine. But now I am asked to automatically return the results without the MES to send me a GET request, I guess to accelerate the processing flow, as order can vary in size, my algorithm also can be fast for small order and take longer for larger orders.

Anyone has some example that would illustrate my case or some pointers to help.

I am using the following

http_client client(server_http_test);
try {
     http_request request_get(methods::GET);
     pplx::task<void> task = client.request(request_get)
    .then([](http_response response)-> pplx::task<web::json::value>
    {
    if(response.status_code() == RESPONSE_OK)
    {
       return response.extract_json();
    }
    else
    {
       return pplx::task_from_result(web::json::value());
    };
    }).then([](pplx::task<web::json::value> previousTask)

I get the JSON using

if(!previousTask.get().is_null()) {
   //const web::json::value & json_value = previousTask.get();
   // Assign the requested Json value to a global variable
    j_value = previousTask.get(); // json_value;
}

then I use my algorithm to process the j_value JSON after parsing it to my own data structure. Now this is where I need to return the results to the same server (IP/port) but without any GET / POST ... just directly ... and I do not know how to achieve that ...

then I finishing the try part

}
catch(const http_exception &e)
{
std::cout << "Response Json Value Level: Error exception: " << e.what() << std::endl;
}
});

My Flask server is called by a python script (from command line as python rest_request_server.py)

@app.route('/DataTransfer', methods=['GET', 'POST'])
def datatransfer():

    global json_data

    if(request.method == 'GET'):
        print ("request.method = GET")
        return jsonify(json_data)

    #if(request.method == 'POST'):
    #   print ("request.method = POST")
    #   return jsonify(json_data_post)

    #if(request.method == 'POST'):
    else:
        print("request.method = POST")
        temp_data = request.get_json()
        if(temp_data != json_data):
            json_data = temp_data
            return "Input captured"
        else:
            return jsonify(json_data)
        #   return "Please send get request"

if __name__ == '__main__':
    app.run(host = ServerIP, port=ServerPort, debug=True,  use_reloader=False)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I end up using only Flask server and calling my packing optimizer directly without the need for it to talk thru REST ... Flask return the results to the MES properly now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...