New to Casablanck SDKs

Sep 1, 2014 at 7:46 AM
I have created a mini light weight Web Server using Casablanca SDK with the help of this link.

http://www.strathweb.com/2012/12/web-api-alternative-self-hosting-http-services-in-native-c-code/

I am able to GET and POST request to the server using Advanced Rest Client in Google Chrome. Now I want to achieve the same result using webpage and Javascript.

I came across this link.

http://www.codeproject.com/Articles/603810/Using-Casablanca-to-consume-a-REST-API

Using the code I am able to GET Request, but while POST I am coming across this error

[code]Request header field Content-Type is not allowed by Access-Control-Allow-Headers.[/code]

How can I make it work? What is the reason for this error?
Coordinator
Sep 3, 2014 at 9:35 PM
Hi maverick,

First off, here's some information on Access-Control-Allow-Headers:

http://www.w3.org/TR/cors/#access-control-allow-headers-response-header
http://www.w3.org/TR/cors/#preflight-request

CORS (Cross Origin Resource Sharing) is a protocol to prevent CSRF (Cross Site Request Forgery). It sounds like you're running afoul of this somewhere along the line. In order to give you a better diagnosis however, I'll need to know more about your setup:
  • Where's the server and what is its domain name (or ip address)?
  • What's the request being made?
  • What is the page making the request (if using javascript)?
  • What is the code using casablanca? If possible, please post a fully compilable snippet.
Sincerely,
roschuma
Sep 4, 2014 at 7:23 AM
Q: Where's the server and what is its domain name (or ip address)?
A: I am using it locally not in some external IP Address, therefore I am using Localhost as the server. So in my C++ Application I am using
uri_builder uri(L"http://localhost:4999/");. I am able to GET, POST and PUT request using Advanced REST SDK client. I want to accomplish the same functionality from a webpage or javascript.

Q: What's the request being made?
A: I want to make GET, POST AND PUT request. Till now I have succeed in making GET request, the issue is while making POST and PUT request.

Q: What is the code using casablanca? If possible, please post a fully compilable snippet.
A: I am using the same source code that is mentioned in this tutorials
http://www.strathweb.com/2012/12/web-api-alternative-self-hosting-http-services-in-native-c-code/

You can download that source code form this GitHub location

https://github.com/filipw/sample-native-cplus-http-service

I can give you a brief overview, what I have written in the main function
// Server.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "astreambuf.h"
#include "Person.h"

using namespace http;
using namespace http::listener;

int _tmain(int argc, _TCHAR* argv[])
{
    uri_builder uri(L"http://localhost:4999/");
    http_listener listener;
    auto p = new Person("Michael","OFlynn",23);
 
    listener = http_listener::create(uri.to_uri(),[p](http_request message)
    {
        std::cout << "Serving GET" << std::endl;
        message.reply(http::status_codes::OK, p->ToJSON());
    },
    [p](http_request message)
    {
        auto posted = p->FromJSON(message.extract_json().get());
        message.reply(http::status_codes::NoContent);
        std::cout << "Received a PUT of " << posted.FirstName << ", " << posted.LastName << ", " << posted.Age << std::endl;
    },
    [p](http_request message)
    {
        auto posted = p->FromJSON(message.extract_json().get());
        std::cout << "Received a POST of " << posted.FirstName << ", " << posted.LastName << ", " << posted.Age << std::endl;
        message.reply(http::status_codes::NoContent);
    },
    [p](http_request message)
    {
        message.reply(http::status_codes::NoContent);
        std::cout << "Deleting " << p->FirstName << std::endl;
    });
 
    listener.listen([]() { fgetc(stdin); }).wait();
    return 0;
}
As mentioned in the tutorial, I am using exactly the same person class
Sep 4, 2014 at 7:33 AM
Coming to your third question

Q: What is the page making the request (if using javascript)?
A: This is the code that I have written in the page. The problem with POST and PUT request was "Access control Headers". Both the code, C++ server code and this html code reside in the same PC. I am able to obtain GET request successfully.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
 <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>


<script type="text/javascript">

    $(document).ready(function () {
        alert('da');
        $.ajax({
            url: "http://localhost:6317/",
            data: JSON.stringify({ "LastName": "O Flynn", "FirstName": "Michael", "Age": 23}),
            type: "get",
            contentType: "application/json",
            dataType: "jsonp",
            jsonpCallback: "localJsonpCallback"
        });

        function localJsonpCallback(json) {
            alert(json.LastName);
        }

    });
</script>
</head>
<body>

</body>
</html>
Coordinator
Sep 4, 2014 at 5:20 PM
Ok, two immediate things that I've noticed:
  1. This tutorial (and therefore your code) is using an incredibly outdated version of the SDK. To get the latest version of Casablanca, use the NuGet package manager in Visual Studio as outlined here. However, I don't think this will solve the core issue.
  2. In your ajax request, you're using JSONP. This is a mechanism to enable cross-domain javascript requests; as long as you're within the same domain you should not need it. In fact, JQuery should simply ignore it while you are in the same domain. Regardless, could you try changing the "dataType" to be "json" and removing the property "jsonpCallback"?
I'm still working on reproducing your issues, but please consider the above points.

Documentation on JSONP and JQuery's $.ajax():
http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
http://api.jquery.com/jQuery.ajax/

Sincerely,
roschuma
Coordinator
Sep 4, 2014 at 9:34 PM
Ok, I've been able to get similar issues as to what you're receiving. The answer is mostly summed up in my previous post and the StackOverflow article here.

Basically, you're working cross-domain even though it's all local. This means you either need to specify Access-Control-Allow-Origin and friends on the Casablanca/server side, or you need to rework your API to only use GET requests and implement JSONP on the Casablanca/server side.

Here's an example that implements JSONP in our listener with Casablanca version 2.2:
    listener.support(http::methods::GET,
        [p](http_request message)
        {
            std::cout << "Serving GET" << std::endl;
            auto query_string = message.absolute_uri().query();
            auto query_map = uri::split_query(query_string);
            auto it = query_map.find(U("callback"));
            if (it != query_map.end())
            {
                // Query uses JSONP
                auto callback = it->second;
                utility::stringstream_t ss;
                ss << callback << "(" << p->ToJSON().serialize() << ")";
                message.reply(http::status_codes::OK, ss.str());
            }
            else
            {
                // Same domain, therefore no JSONP
                message.reply(http::status_codes::OK, p->ToJSON());
            }
    });
This is compatible with using a JQuery request of the form
        $.ajax({
          url: "http://localhost:4999/",
          jsonp: "callback",
          dataType: "jsonp",
          crossDomain: true,
          data: { "LastName": "O Flynn", "FirstName": "Michael", "Age": 23 },

          success: function (json) {
            $("body").html("Hello: " + json.FirstName);
          }
        });
Note that you do not need to call JSON.stringify() on the data element.

I hope this solves your issue and I'd like to say thank you for the all the details you've provided; they made reproducing this issue much easier.

Sincerely,
roschuma
Sep 5, 2014 at 6:16 AM
Edited Sep 5, 2014 at 8:48 AM
roschuma wrote:
Ok, I've been able to get similar issues as to what you're receiving. The answer is mostly summed up in my previous post and the StackOverflow article here.

Basically, you're working cross-domain even though it's all local. This means you either need to specify Access-Control-Allow-Origin and friends on the Casablanca/server side, or you need to rework your API to only use GET requests and implement JSONP on the Casablanca/server side.

Here's an example that implements JSONP in our listener with Casablanca version 2.2:
    listener.support(http::methods::GET,
        [p](http_request message)
        {
            std::cout << "Serving GET" << std::endl;
            auto query_string = message.absolute_uri().query();
            auto query_map = uri::split_query(query_string);
            auto it = query_map.find(U("callback"));
            if (it != query_map.end())
            {
                // Query uses JSONP
                auto callback = it->second;
                utility::stringstream_t ss;
                ss << callback << "(" << p->ToJSON().serialize() << ")";
                message.reply(http::status_codes::OK, ss.str());
            }
            else
            {
                // Same domain, therefore no JSONP
                message.reply(http::status_codes::OK, p->ToJSON());
            }
    });
This is compatible with using a JQuery request of the form
        $.ajax({
          url: "http://localhost:4999/",
          jsonp: "callback",
          dataType: "jsonp",
          crossDomain: true,
          data: { "LastName": "O Flynn", "FirstName": "Michael", "Age": 23 },

          success: function (json) {
            $("body").html("Hello: " + json.FirstName);
          }
        });
Note that you do not need to call JSON.stringify() on the data element.

I hope this solves your issue and I'd like to say thank you for the all the details you've provided; they made reproducing this issue much easier.

Sincerely,
roschuma
Thanks Roschuma for the help. Your code looks promising. There is just one small issue I am facing. Here is my code.

``` int _tmain(int argc, _TCHAR* argv[])
{
uri_builder uri(L"http://172.29.15.63:4999/");
http_listener listener;
auto p = new Person("Michael","OFlynn",23);

listener = http_listener::create(uri.to_uri(),[p](http_request message)
{
    std::cout << "Serving GET" << std::endl;
    message.reply(http::status_codes::OK, p->ToJSON());
},
[p](http_request message)
{
    auto posted = p->FromJSON(message.extract_json().get());
    message.reply(http::status_codes::NoContent);
    std::cout << "Received a PUT of " << posted.FirstName << ", " << posted.LastName << ", " << posted.Age << std::endl;
},
[p](http_request message)
{
    auto posted = p->FromJSON(message.extract_json().get());
    std::cout << "Received a POST of " << posted.FirstName << ", " << posted.LastName << ", " << posted.Age << std::endl;
    message.reply(http::status_codes::NoContent);
},
[p](http_request message)
{
    message.reply(http::status_codes::NoContent);
    std::cout << "Deleting " << p->FirstName << std::endl;
});

listener.support(http::methods::GET, [p](http_request message)
{
    std::cout << "Serving GET" << std::endl;
    auto query_string = message.absolute_uri().query();
    auto query_map = uri::split_query(query_string);
    auto it = query_map.find(U("callback"));
    if (it != query_map.end())
    {
        // Query uses JSONP
        auto callback = it->second;
        utility::stringstream_t ss;
        ss << callback << "(" << p->ToJSON().serialize() << ")";
        message.reply(http::status_codes::OK, ss.str());
    }
    else
    {
        // Same domain, therefore no JSONP
        message.reply(http::status_codes::OK, p->ToJSON());
    }

});

listener.listen([]() { fgetc(stdin); }).wait();
return 0;
}

The lines that I have highlighted in bold



__auto query_string = message.absolute_uri().query();
utility::stringstream_t ss;
ss << callback << "(" << p->ToJSON().serialize() << ")";

__


1) It is unable to find absolute_uri as the member of http_request
2) It is unable to find a utility class because of which rest of the 2 lines show error.

Can you help me in resolve these 2 issues?

Thanks in Advance
Coordinator
Sep 5, 2014 at 9:41 PM
Ok, first I'm assuming you're using the latest version (2.2).

Then, the top of my main file looks like
#include "stdafx.h"
#include <iostream>
#include "cpprest/astreambuf.h"
#include "cpprest/http_listener.h"
#include "Person.h"

using namespace web;
using namespace http;
using namespace http::experimental::listener;
roschuma
Sep 8, 2014 at 7:44 AM
Edited Oct 1, 2014 at 10:10 AM
roschuma wrote:
Ok, first I'm assuming you're using the latest version (2.2).

Then, the top of my main file looks like
#include "stdafx.h"
#include <iostream>
#include "cpprest/astreambuf.h"
#include "cpprest/http_listener.h"
#include "Person.h"

using namespace web;
using namespace http;
using namespace http::experimental::listener;
roschuma
Hi Rosch, thanks for he help once agian. yes I am using the latest version of REST SDK which I updated using NetGet Package manager.

Image

After including those header files, these are the errors that I have come across. Can you help in resolving these errors?
Error 1 error C2371: 'SafeSize' : redefinition; different basic types d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\basic_types.h 44
Error 3 error C2039: 'atomic_long' : is not a member of 'pplx::details' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 684
Error 4 error C2146: syntax error : missing ';' before identifier 'm_initiated_response' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 684
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 684
Error 6 error C3083: 'extensibility': the symbol to the left of a '::' must be a type d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1326
Error 7 error C2039: 'recursive_lock_t' : is not a member of 'pplx' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1326
Error 8 error C2146: syntax error : missing ';' before identifier 'm_lock' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1326
Error 9 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1326
Error 10 error C3083: 'extensibility': the symbol to the left of a '::' must be a type d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1276
Error 11 error C2039: 'scoped_recursive_lock_t' : is not a member of 'pplx' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1276
Error 12 error C2065: 'scoped_recursive_lock_t' : undeclared identifier d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1276
Error 13 error C2146: syntax error : missing ';' before identifier 'l' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1276
Error 14 error C2065: 'm_lock' : undeclared identifier d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1276
Error 15 error C3861: 'l': identifier not found d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1276
Error 16 error C3083: 'extensibility': the symbol to the left of a '::' must be a type d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1288
Error 17 error C2039: 'scoped_recursive_lock_t' : is not a member of 'pplx' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1288
Error 18 error C2065: 'scoped_recursive_lock_t' : undeclared identifier d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1288
Error 19 error C2146: syntax error : missing ';' before identifier 'l' d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1288
Error 20 error C2065: 'm_lock' : undeclared identifier d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1288
Error 21 error C3861: 'l': identifier not found d:\maverick\projects\Striker\src\server\packages\cpprestsdk.2.2.0\build\native\include\cpprest\http_msg.h 1288
Error 22 error C2664: 'std::_Tree_iterator<_Mytree> std::_Tree<_Traits>::find(const http::uri::encoded_string &)' : cannot convert parameter 1 from 'const wchar_t [9]' to 'const http::uri::encoded_string &' d:\maverick\projects\Striker\src\server\server\server.cpp 50
Error 23 error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'int' (or there is no acceptable conversion) d:\maverick\projects\Striker\src\server\server\server.cpp 52
Error 24 error C2227: left of '->second' must point to class/struct/union/generic type d:\maverick\projects\Striker\src\server\server\server.cpp 55
Error 25 error C2661: 'http::json::value::serialize' : no overloaded function takes 0 arguments d:\maverick\projects\Striker\src\server\server\server.cpp 57
Error 26 error C3536: 'it': cannot be used before it is initialized d:\maverick\projects\Striker\src\server\server\server.cpp 52
Error 27 error C2446: '!=' : no conversion from 'std::_Tree_iterator<_Mytree>' to 'int' d:\maverick\projects\Striker\src\server\server\server.cpp 52
Error 28 error C2088: '!=' : illegal for class d:\maverick\projects\Striker\src\server\server\server.cpp 52
Error 29 error C3536: 'it': cannot be used before it is initialized d:\maverick\projects\Striker\src\server\server\server.cpp 55
Error 30 error C3536: 'callback': cannot be used before it is initialized d:\maverick\projects\Striker\src\server\server\server.cpp 57
31  IntelliSense: no instance of overloaded function "std::map<_Kty, _Ty, _Pr, _Alloc>::find [with _Kty=http::uri::encoded_string, _Ty=http::uri::encoded_string, _Pr=std::less<http::uri::encoded_string>, _Alloc=std::allocator<std::pair<const http::uri::encoded_string, http::uri::encoded_string>>]" matches the argument list
        argument types are: (const wchar_t [9])
        object type is: std::map<http::uri::encoded_string, http::uri::encoded_string, std::less<http::uri::encoded_string>, std::allocator<std::pair<const http::uri::encoded_string, http::uri::encoded_string>>> d:\Maverick\Projects\Striker\SRC\Server\Server\Server.cpp   50
32  IntelliSense: no instance of overloaded function "http::json::value::serialize" matches the argument list
        object type is: http::json::value   d:\Maverick\Projects\Striker\SRC\Server\Server\Server.cpp   57

Sep 8, 2014 at 9:56 AM
Edited Sep 8, 2014 at 11:06 AM
What I have noticed is , its the inclusion of these header files that is causing this error.

//#include "cpprest/astreambuf.h"
//#include "cpprest/http_listener.h"

Shall I upload the code in GitHub or DropBox, so that you can view it entierly?

You can find the entire source code in this location in GitHub, or can you send me your source code, which is working?

https://github.com/maverick786us/Server/blob/master/Server.zip
https://github.com/maverick786us/Server/archive/master.zip
Coordinator
Sep 9, 2014 at 3:19 AM
I noticed in your stdafx.h file, you have the line
#include <http_listener.h>
https://github.com/maverick786us/Server/blob/master/Server/Server/stdafx.h#L12

This will include outdated files which ship with Visual Studio and conflict with the more up-to-date NuGet package. Try removing this line and any other similar includes.
Sep 9, 2014 at 4:12 AM
Edited Sep 9, 2014 at 6:27 AM
OK I removed
#include <http_listener.h>
But by doing that it is simply unable to identify http_listener class, despite the fact that it is available in cpprest/http_listener.h header file which belongs to NuGet package, resulting with the errors

Is there any different namespace that has to be inlucded for cpprest/http_listener.h header file that is a part of NuGet package?
Coordinator
Sep 10, 2014 at 5:33 PM
I've spent some time attempting to update the project you've posted to use the new APIs, however I think it will be simply easier if you remake it from scratch. Do not follow the tutorial you've mentioned before; pretty much every part of that tutorial no longer applies.

Instead, please look at:
Also, you do not need the "cpprestsdk.redist" NuGet package; follow the steps listed on https://casablanca.codeplex.com/wikipage?title=Using%20NuGet%20to%20add%20the%20C%2b%2b%20REST%20SDK%20to%20a%20VS%20project&referringTitle=Documentation.

Finally, if possible, I recommend updating to Visual Studio 2013 if possible: http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx

Sincerely,
roschuma
Sep 11, 2014 at 4:05 AM
Edited Sep 11, 2014 at 4:07 AM
roschuma wrote:
I've spent some time attempting to update the project you've posted to use the new APIs, however I think it will be simply easier if you remake it from scratch. Do not follow the tutorial you've mentioned before; pretty much every part of that tutorial no longer applies.

Instead, please look at:
Also, you do not need the "cpprestsdk.redist" NuGet package; follow the steps listed on https://casablanca.codeplex.com/wikipage?title=Using%20NuGet%20to%20add%20the%20C%2b%2b%20REST%20SDK%20to%20a%20VS%20project&referringTitle=Documentation.

Finally, if possible, I recommend updating to Visual Studio 2013 if possible: http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx

Sincerely,
roschuma
Thanks Rosch. I will try these set of steps and let you know when it works. For me the biggest challange that I am facing is, I am also new to Visual Studio 2012 - 2013 where C++ is entirely different. In the meantime if possible can you give me a complete working source code of an HTTP listner which takes these request from the webpage? As I am running short of time?
Coordinator
Sep 11, 2014 at 9:38 PM
Here's a simple one-file example:
#include <iostream>
#include <cpprest/http_listener.h>

using namespace web::http::experimental::listener;
using namespace web::http;

int main()
{
    uri_builder uri(L"http://localhost:2001/");
    http_listener listener(uri.to_uri());

    listener.support(methods::GET, [](http_request req)
    {
        std::cout << "Serving GET" << std::endl;
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is.)</body></html>"), U("text/html"));
    });
    
    listener.open().wait();
    fgetc(stdin);
    listener.close().wait();
    return 0;
}
Sep 12, 2014 at 5:08 AM
Edited Oct 1, 2014 at 10:11 AM
roschuma wrote:
Here's a simple one-file example:
#include <iostream>
#include <cpprest/http_listener.h>

using namespace web::http::experimental::listener;
using namespace web::http;

int main()
{
    uri_builder uri(L"http://localhost:2001/");
    http_listener listener(uri.to_uri());

    listener.support(methods::GET, [](http_request req)
    {
        std::cout << "Serving GET" << std::endl;
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is.)</body></html>"), U("text/html"));
    });
    
    listener.open().wait();
    fgetc(stdin);
    listener.close().wait();
    return 0;
}
Thanks. That is for GET. Can you also how me an example for POST? Additionally which .LIB file is used for Http Listener? Because I am coming across these errors?
Sep 14, 2014 at 5:00 PM
Edited Sep 15, 2014 at 11:08 AM
Rosch, please ignore my previous message. I got the error corrected. Since I am new to VS 2012 and 2013, I didn't know that for every project there is option of updating the SDK.

Can you just give me an example for POST, where I am able to POST some value from a string from a webpage to this server?
Coordinator
Sep 15, 2014 at 7:42 PM
Just change GET in the above snippet to POST. The API for http_listener can be found here. Specifically, look at the "support" function.
Sep 16, 2014 at 5:34 AM
Yes I am able to do that.
listener.support(methods::POST,[](http_request req)
    {
        //auto posted = p->FromJSON(req.extract_json().get());
        std::cout << "Serving POST" << std::endl;
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a PUT Request.)</body></html>"), U("text/html"));
    });
What I am looking for is posting a string from webpage to the listener.
Coordinator
Sep 16, 2014 at 5:27 PM
The easiest way is probably a <form> html tag.

http://www.w3schools.com/tags/tag_form.asp

However, this is getting outside Casablanca and more into web design -- there are many resources online that are much more suited to explain HTML/JS than we are. I'd recommend starting with searching the w3schools website for more information.
Sep 17, 2014 at 4:12 AM
Edited Sep 17, 2014 at 5:59 AM
roschuma wrote:
The easiest way is probably a <form> html tag.

http://www.w3schools.com/tags/tag_form.asp

However, this is getting outside Casablanca and more into web design -- there are many resources online that are much more suited to explain HTML/JS than we are. I'd recommend starting with searching the w3schools website for more information.
I didnt ask for help related to web or form design. I already have a webpage, which is sending a string using Ajax. What i want is receive the value of that string over here, which has been posted by that webpage.

I have this code.
 $.ajax({
          url: "http://localhost:2001/",
          dataType: "json",
          type:"post",
          data: {Name:"Mike"},
          success: function (data) {
              console.log(data);
          }
      });
Now in this case I am using a string to pass the name 'Mike', this value of the name I want to POST to my receiver application so that, it can receive there. So in the receiver application, how can I receive this string that was POSTED from the above mentioned code?
Coordinator
Sep 18, 2014 at 10:04 PM
The request object has functions for extracting the body of the request as a string or as json.

http://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1http__request.html

I suspect that
auto v = req.extract_json().get();
will work.
Sep 22, 2014 at 5:27 AM
Edited Sep 22, 2014 at 10:38 AM
I am using this code
 $.ajax({
          url: "http://localhost:2001/",
          dataType: "json",
          type:"post",
          data: {Name:"Roschuma"},
          success: function (data) {
              console.log(data);
          }
      });
and then in listner I hae used your code
    listener.support(methods::POST,[](http_request req)
    {
        //auto v = req.extract_json().get();
        auto v1 = req.extract_string();
        req.reply(status_codes::OK, U("<html><body><h1>It works!</h1>(Casablanca, that is a PUT Request.)</body></html>"), U("text/html"));
        std::cout << v1.get().c_str() << std::endl;

    }); 
But while debugging when I trace the value of v1 instead of showing your name, it shows some binary value.

Can you please provide me an example where I can simple pass a string from the webpage which this listener receives. My job will be at stake if I fail to accomplish this
Coordinator
Sep 22, 2014 at 9:36 PM
Looking at the API I've posted above, extract_string() returns a pplx::task<utility::string_t>. string_t, on Windows, is a wide-string (UTF-16). This means you'll need to use std::wcout to output it. This is probably why you're getting binary garbage.

The following works fine for me:
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"));
    });
});
Marked as answer by maverick786us on 9/22/2014 at 11:48 PM
Sep 23, 2014 at 6:49 AM
Thanks Rosch, it finally worked.
Marked as answer by maverick786us on 9/24/2014 at 9:43 PM