restbed 实现http 客户端

  多用一个库,多一个坑,用一个库就要用足它的红利,可能的情况下尽量采纳同一个库。前面采用来restbed 来实现websocket 和http server ,今天需要写一个influxdb 的C++客户端。有一次选用restbed 来作为 http客户端。网上建议比较多的是微软的 C++ REST SDK (https://github.com/Microsoft/cpprestsdk),我还是使用老伙计restbed。

#include <memory>
#include <future>
#include <cstdio>
#include <cstdlib>
#include <restbed>
#include <string>
#include <iostream>
using namespace std;
using namespace restbed;

int main( const int, const char** )
{
    string messsgae_body="{\"hello\":\"yao\"}";
    auto request = make_shared< Request >( Uri( "http://localhost:8000/hello" ) );
   request->add_header("Content-Type","application/json");
   request->add_header("Content-Length",to_string(messsgae_body.length()));
     request->set_body(messsgae_body);
    auto response = Http::sync( request );

    auto length = response->get_header( "Content-Length", 0 );
    Http::fetch( length, response );
    string response_body((char*) response->get_body().data());
    cout<<"response:"<<response_body<<endl; 

    return EXIT_SUCCESS;
}

很简单吧?

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值