CPPRESTSDK (Casablanca) not responding to client

33 Views Asked by At

I am working on a server on C++ that would receive and process requests and I am using cpprestsdk for that (as a common solution), but my responses never reach my client for some reason

Server code:

    listener.support(web::http::methods::POST, [](web::http::http_request request) {
        std::cout << "request received" << std::endl;
        if (request.headers().content_type() != U("application/json")) {
            request.reply(web::http::status_codes::UnsupportedMediaType, U("Expected JSON data."));
            return;
        }

        request.extract_json().then([request](pplx::task<web::json::value> dataTask) {
            // Create a RequestHandler instance
            RequestHandler requestHandler;

            // Handle the request asynchronously using .then
            dataTask.then([request, &requestHandler](web::json::value data) {
                web::json::value response = requestHandler.handleRequest(data);

                web::http::http_response httpResponse(web::http::status_codes::OK);
                httpResponse.headers().set_content_type(U("application/json"));
                httpResponse.set_body(response);

                // Send the response
                std::cout << "Response sent" << std::endl;
                request.reply(200,response);
                std::cout << "A hundred percent sent" << std::endl;
                PrintJson(response);
                });
            });
        });

Client code

        std::vector<char> buffer(1024); 
        std::string response;

        while (true) {
            size_t bytes_transferred = socket.read_some(asio::buffer(buffer));
            if (bytes_transferred == 0) {
                break; // No more data to read
            }

            response.append(buffer.begin(), buffer.begin() + bytes_transferred);
        }


        size_t content_type_pos = response.find("Content-Type: ");
        if (content_type_pos != std::string::npos) {
            content_type_pos += 14; // Length of "Content-Type: "
            size_t content_type_end = response.find("\r\n", content_type_pos);
            std::string content_type = response.substr(content_type_pos, content_type_end - content_type_pos);

            if (content_type == "application/json") {

                std::stringstream ss(response);
                pt::ptree json_response;
                pt::read_json(ss, json_response);

                std::string responseMessage = json_response.get<std::string>("Response");
                std::string csrf = json_response.get<std::string>("CSRF");
                std::string token = json_response.get<std::string>("TOKEN");

                std::cout << "Response: " << responseMessage << std::endl;
                std::cout << "CSRF: " << csrf << std::endl;
                std::cout << "TOKEN: " << token << std::endl;
            }
            else if (content_type == "text/plain") {

                size_t text_data_pos = response.find("\r\n\r\n") + 4;
                std::string text_response = response.substr(text_data_pos);
                std::cout << "Text Response:\n" << text_response << std::endl;
            }
            else {
                std::cout << "Unsupported media type: " << content_type << std::endl;
            }
        }
        else {
            std::cout << "Content-Type header not found in response" << std::endl;
        }

        // Output "response received" message
        std::cout << "Response received" << std::endl;
    }

The output i usually get on the server :

Listening on http://localhost:8080 request received Response sent A hundred percent sent Received JSON: {"Error":"Invalid function name: sth"}

The output on the client :

Exception: read_some: An existing connection was forcibly closed by the remote host [system:10054]

0

There are 0 best solutions below