1

I am trying to chain some REST requests using restbed lib and I have an issue. So the work flow is something like this: the frontend sends a GET request to the backend. The backend does some processing and should return a reponse to the frontend but in the same time it should also POST the resposnse to another REST server.

void CCMService::get_method_handler(const shared_ptr< Session > session)
{
    const auto request = session->get_request();

    int content_length = request->get_header("Content-Length", 0);

    session->fetch(content_length, [](const shared_ptr< Session > session, const Bytes & body)
    {
        std::vector<std::string> resultImages;
        fprintf(stdout, "%.*s\n", (int)body.size(), body.data());
        const auto request = session->get_request();

        const string parameter = request->get_path_parameter("camGroupId");
        try
        {
            resultImages = prepareImages(parameter.c_str());
        }
        catch (const std::exception& e)
        {
            std::string error = e.what();
            std::string message = "{error: \"" + error + "\"}";
            throw std::exception(message.c_str());
        }

        fprintf(stderr, "Return response\n");
        session->close(OK, resultImages[0], { { "Content-Length",  std::to_string(resultImages[0].length())} });
        fprintf(stderr, "Send tiles to inference\n");
        //send POST request
        sendResult(resultImages[1]);
    });
}

void CCMService::sendResult(char* result)
{
    auto request = make_shared< Request >(Uri("http://127.0.0.1:8080/api"));

    request->set_header("Accept", "*/*");
    request->set_header("Content-Type", "application/json");
    request->set_method("POST");
    request->set_header("Host", "http://127.0.0.1:8080");
    //request->set_header("Cache-Control", "no-cache");

    ...
    //create json from result - jsonContent
    ...

    request->set_header("Content-Length", std::to_string(jsonContent.length()));
    request->set_body(jsonContent);

    auto settings = make_shared< Settings >();

    auto response = Http::sync(request, settings);
    print(response)
}

What happens is that when I do the POST request from sendResult function it immediately gets a error response and does not wait for the real response. What am I doing wrong? Thanks.

Alex
  • 37
  • 1
  • 8
  • The client & service side have little interaction with respect to chaining. This will be addressed in the 5.0. Please raise a feature request on the GitHub page. – Ben Crowhurst Feb 20 '18 at 09:58

0 Answers0