Quantcast
Channel: WE MOVED to github.com/microsoft/cpprestsdk. This site is not monitored!
Viewing all articles
Browse latest Browse all 4845

New Post: New to Casablanck SDKs

$
0
0
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

Viewing all articles
Browse latest Browse all 4845


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>