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

New Post: http_client sending json

$
0
0
hi ohhysobe,
#include <cpprest/http_client.h>
#include <cpprest/json.h>

using namespace web;
using namespace web::http;
using namespace web::http::client;

#include <iostream>

using namespace std;

void display_field_map_json(json::value & jvalue)
{
    if(!jvalue.is_null())
    {
        for (auto const & e : jvalue)
        {
            cout << e.first.as_string() << " : " << e.second.as_string() << endl;
        }
    }
}

pplx::task<http_response> make_task_request(http_client & client,
        method mtd,
        json::value const & jvalue) {

    return (mtd == methods::GET || mtd == methods::HEAD) ?
            client.request(mtd, "/restdemo") :
            client.request(mtd, "/restdemo", jvalue);
}

void make_request(http_client & client, method mtd, json::value const & jvalue) {
    make_task_request(client, mtd, jvalue)
            .then([](http_response response) {
                if (response.status_code() == status_codes::OK) {
                    return response.extract_json();
                }
                return pplx::task_from_result(json::value());
            })
    .then([](pplx::task<json::value> previousTask) {
        try {
            display_field_map_json(previousTask.get());
        } catch (http_exception const & e) {
            wcout << e.what() << endl;
        }
    })
    .wait();
}

int main() {
    http_client client("http://192.168.2.102:8000");

    json::value::field_map putvalue;
    
    putvalue.push_back(make_pair(json::value(L"one"), json::value(L"100")));
    putvalue.push_back(make_pair(json::value(L"two"), json::value(L"200")));

    cout << "\nput values\n";
    make_request(client, methods::PUT, json::value::object(putvalue));

    return 0;
}
Thanks for helping
hsgimit

Viewing all articles
Browse latest Browse all 4845

Trending Articles



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