c++httpvirtual-machinevirtualboxcasablanca

Sending http request to server running on a VirtualBox linux machine from the host machine


I currently am developing a REST server using Microsoft's cpprestsdk called Casablanca.

I have the server running on a linux virtual machine using Oracle's VirtualBox.

I have the VM set up to use bridged adapter networking, and can successfully SSH into the machine so I know that it is possible to send an http request to my server.

I have the endpoint of my server currently set to:

http://localhost:4200/api"

See the code in my main.cpp below:

int main() {
    cout << "Starting Server" << endl;

    TransactionController server;
    server.setEndpoint("http://localhost:4200/api");
    server.initHandlers();

    try {
        server.openServer().wait();
        cout << "Server listening at: " << server.getEndpoint() << endl;

        // figure out how to keep server running without this?
        while (true);
    }
    catch(exception &e) {
        cout << "--- ERROR DETECTED ---" << endl;
        cout << e.what() << endl;
    }


    // this doesn't get reached bc of the while(true)
    server.closeServer().wait();

    return 0;
}

(I know that this isn't the cleanest implementation, but I am just trying to get something off the ground so I can test functionality, feel free to comment on how I can improve that code snippet if you have anything to say)

So if I log into my VM and do a curl GET request on the guest machine it completes successfully and I receive my response as intended.

Example curl:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:4200/api

Now my question is, how can I do the same request but from my host computer, using an HTTP client such as Postman or Advanced Rest Client?

I am not sure what I would put as the request URL when attempting to query my server running on my guest machine from my host machine.

Using ifconfig I know my ip address of the guest machine is:

10.0.0.157

I can SSH into my VM using this address, so I know this is the correct address of my guest machine.

I do not know how I can send my http requests to this machine that is running my server, however.

I am not an expert in networking, or casablanca for that matter, so any guidance or pointers in the right direction would be greatly appreciated. Thank you for your time!


Solution

  • You've bound your server to localhost so it is only available from localhost. I don't know what TransactionController::setEndpoint does but you probably need to do one of the following:

    server.setEndpoint("http://10.0.0.157:4200/api"); // bind to only 10.0.0.157
    server.setEndpoint("http://0.0.0.0:4200/api"); // bind to all ipv4 adresses
    server.setEndpoint("http://*:4200/api"); // bind to all addresses
    

    Which of the above will work will depend on the implementation of setEndpoint.