Sending GET Request

Apr 1, 2015 at 8:04 AM
Edited Apr 2, 2015 at 1:08 PM
I have created a listener application using Latest Casablanca SDK. This is the source code.

for GET request
listener.support(methods::GET, [](http_request req)
    {
        HKEY hKey;
        LONG lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Casablanca", 0, KEY_READ, &hKey);

        bool bExistsAndSuccess (lRes == ERROR_SUCCESS);
        bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND);

        std::wstring strValueOfBinDir;
        GetStringRegKey(hKey, L"Encryption", strValueOfBinDir, L"1");

        GIDSdkWriteXML();

        http_response response (status_codes::OK);

        response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
        //response.set_body(strValueOfBinDir, U("text/html"));
        response.set_body(L"Welcome to Casablanca", U("text/html"));

        req.set_request_uri(L"requestpath");
        req.reply(response);
    });
For PUT request
listener.support(methods::PUT,[](http_request req)
    {
        std::cout << "Serving PUT" << std::endl;
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a POST Request.)</body></html>"), 
            U("text/html"));
    });
for POST request
listener.support(methods::POST,[](http_request req)
    {
        std::cout << "Serving POST" << std::endl;
        req.extract_string(true).then([req](utility::string_t body)
        {
            std::wcout << body << std::endl;
            //req.reply(status_codes::OK, U("<html><body>Received.</body></html>"), U("text/html"));
        });
    }); 
The GET requst is send by browser plugins and from a web service, that is running remotely. Is there a way, I can use conditional statement, to determine, that if GET request is called from browser plugin, a different response should come and in case of web service, a different response should come?
Coordinator
Apr 1, 2015 at 4:42 PM
Hi maverick786us,

You could perhaps try to identify the client making the request by the User Agent HTTP header, but this isn't guaranteed to work in all cases. The other option is assume it is a browser unless otherwise indicated and have the web service identify itself.

Steve
Apr 2, 2015 at 10:02 AM
stevetgates wrote:
Hi maverick786us,

You could perhaps try to identify the client making the request by the User Agent HTTP header, but this isn't guaranteed to work in all cases. The other option is assume it is a browser unless otherwise indicated and have the web service identify itself.

Steve
@Steve, I just came up with this idea, but need your help and suggestion. If you take a look at this code.
This is the post request, which displays, what we post from a browser addin or the web service.
listener.support(methods::POST,[](http_request req)
    {
        std::cout << "Serving POST" << std::endl;
        req.extract_string(true).then([req](utility::string_t body)
        {
            std::wcout << body << std::endl;
            std::wcout << "End of Body" << std::endl;
            //req.reply(status_codes::OK, U("<html><body>Received.</body></html>"), U("text/html"));
        });
    }); 
So lets say, that when I am hitting the URL from we browser addin, I POST something else and while hitting the URL from web service, I POST something else and based on the condition that, what string was POSTED, I use it in GET method. But in order to accomplish this I hate to pass POST request first and then the GET response. What do you think of it?
Coordinator
Apr 13, 2015 at 10:29 PM
Hi maverick786us,

CodePlex has been down for a while so I haven't been able to respond :(

I don't really completely follow what you mean, but if you have a POST request that updates a string and other GET requests are retrieving the string at the same time it would be race condition as to which request gets processed first. There for the GET requests might be the value before or after the POST.

Steve
Apr 28, 2015 at 6:35 AM
Edited Apr 28, 2015 at 11:23 AM
stevetgates wrote:
Hi maverick786us,

CodePlex has been down for a while so I haven't been able to respond :(

I don't really completely follow what you mean, but if you have a POST request that updates a string and other GET requests are retrieving the string at the same time it would be race condition as to which request gets processed first. There for the GET requests might be the value before or after the POST.

Steve
I am making the web service to first send a post request, and then get retrive the GET request, so it obvious that POST request will come first. Now based on the condition, what was POSTED in string, I want this section of code return different values in GET request.
response.set_body(L"Welcome to Casablanca", U("text/html"));
I tried out something, but for that I need some help. In this code...
listener.support(methods::POST,[](http_request req)
    {
        std::cout << "Serving POST" << std::endl;
        req.extract_string(true).then([req](utility::string_t body)
        {
            std::wcout << body << std::endl;
        });
    }); 
The value that I am passing from the agent and the web service are stored in this string string_t body. Now this value I want to store in a local variable, and want to use it in this section of the code..
listener.support(methods::GET, [](http_request req)
    {
        HKEY hKey;
        LONG lRes = RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\StrikeForce", 0, KEY_READ, &hKey);

        bool bExistsAndSuccess (lRes == ERROR_SUCCESS);
        bool bDoesNotExistsSpecifically (lRes == ERROR_FILE_NOT_FOUND);

        std::wstring strValueOfBinDir;
        GetStringRegKey(hKey, L"Encryption", strValueOfBinDir, L"1");

        http_response response (status_codes::OK);

        response.headers().add(U("Access-Control-Allow-Origin"), U("*"));
        response.set_body(L"Welcome to Casablanca", U("text/html"));

        req.set_request_uri(L"requestpath");
        req.reply(response);
    });
Where this line "response.set_body(L"Welcome to Casablanca", U("text/html"));" the text present should have different results, based on the value of that global variable, that comes from the POST request.

How can I accomplish this?