Implementing headers

Oct 13, 2014 at 8:17 AM
I have created a lisetener application using latest Casablanca SDK in VC++ 2012. This is my source code. This listener application takes GET, POST and PUT requests
int _tmain(int argc, _TCHAR* argv[])
{
    uri_builder uri(L"http://localhost:2001/");
    http_listener listener(uri.to_uri());

    json::value postData;
    postData[L"Name"] = json::value::string(L"Joe Smith");

    std::string strName; 
    std::cout << "Welcome to Casablanca" << std::endl;

    listener.support(methods::GET, [](http_request req)
    {
        //utility::string_t strName1;
        //ucin >> strName1;

        //std::cout << "Serving GET" << std::endl;
        req.reply(status_codes::OK, U("Welcome to Casablanca"), U("text/html"));
        //req.reply(status_codes::OK, strName1, U("text/html"));
        //req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a GET Request.)</body></html>"), U("text/html"));
    });

    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"));
    });

    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"));
        });
    }); 

    listener.open().wait();
    fgetc(stdin);
    listener.close().wait();
}
Now i have implement “ Access-Control-Allow-Origin: * “ header element with my listener application. How can I accomplish this?

Access Control for Cross-Site Requests
http://www.w3.org/TR/2008/WD-access-control-20080912/

“Access-Control-Allow-Origin” element:
http://www.w3.org/TR/2008/WD-access-control-20080912/#access-control-allow-origin
Oct 13, 2014 at 9:04 AM
I tried something like this.
req.headers().add(L"Access Control for Cross-Site Requests", L"MyHeaderValue");
req.set_request_uri(L"requestpath");
req.reply(status_codes::OK, U("Welcome to StrikeForce"), U("text/html"));
I am not sure how much correct it is, but what should I put in the value?
Oct 13, 2014 at 5:36 PM
Hi maverick786us,

In your code snippet you are adding headers to the http_request object that already came into the listener. It sounds like instead you want them to be on the HTTP response, no? Construct an http_response object and call http_response::headers().add(...). Then you can reply to the request using http_request::reply(...).

Steve
Oct 14, 2014 at 5:18 AM
Edited Oct 14, 2014 at 1:39 PM
**stevetgates wrote:**
Hi maverick786us,

In your code snippet you are adding headers to the http_request object that already came into the listener. It sounds like instead you want them to be on the HTTP response, no? Construct an http_response object and call http_response::headers().add(...). Then you can reply to the request using http_request::reply(...).

Steve
Thanks Steve, can you demonstrate it with a source code? I wrote something like this. Now how can I link this http_response with http_request?

``` listener.support(methods::GET, [](http_request req)
{
    http_response response (status_codes::OK);
    response.headers().add(U("Access-Control-Allow-Origin: *"), U("text/html"));
    response.set_body(U("Welcome to Casablanca - Server"),U("text/html"));

    req.set_request_uri(L"requestpath");

    req.reply(response);
});```
Oct 14, 2014 at 5:30 PM
Hi maverick786us,

That looks correct to me. When calling http_request::reply(http_response) you are indicating to associate the http_response with that request and to send it to the client.

Steve
Marked as answer by maverick786us on 10/16/2014 at 5:45 AM