トルネード

毛利のメモ書き

Visual C++ 2015 cpprestsdkを使ってREST受信

Visual C++ 2015 cpprestsdkを使ってREST受信する

マイクロソフトさんがgithubで公開しているREST SDKです
github.com/Microsoft/cpprestsdk

NuGetからcpprestsdkが取れます

f:id:mojeld:20161027172348j:plain
How to use the C Rest SDK NuGet package · Microsoft/cpprestsdk Wiki · GitHub

方法: HTTP サーバーに接続する (C++ REST SDK)
JSONを操作するライブラリも付いてます
方法: JSON データを操作する (C++ REST SDK)
f:id:mojeld:20161027171809j:plain

試したソース

フリープラン- 駅すぱあとWebサービス - システム連携サービス - サービス・製品紹介 - 駅すぱあとワールド
上記のフリープランデータの一部をREST取得試してみました。

#include "stdafx.h"
#include <cpprest/http_client.h>
#include <locale.h>

#include "eki_key.h"

int main()
{
    using namespace web::http;
    std::locale::global(std::locale("Japanese_Japan.932"));
    setlocale(LC_ALL, std::locale().c_str());
    client::http_client c1{ EKI_URL  L"/v1/json/station/light" };

    //web::http::uri_builder
    uri_builder url1{ uri_builder() 
        .append_query(L"key", EKI_KEY)
        .append_query(L"code", L"29090")
    };

    std::wstring s1 = url1.to_string();
    std::wcout << s1 << std::endl;

    //pplx::task<web::http::http_response> 
    auto t1{ c1.request(web::http::methods::GET, s1) };

    t1.then([](http_response res) {
        std::wstring s1{ res.extract_string().get().c_str() };
        wprintf(s1.c_str() );
    }).wait();

    
    return 0;
}

結果

f:id:mojeld:20161027172710j:plain
JSONの文字列が取得できました。