Giter Club home page Giter Club logo

restclient-cpp's Introduction

MIT license

关于

这是一个用于C++的简单便捷的REST Client组件,它包装了windows (winhttp/winnet) api !
支持代理,http BasicAuth 认证

环境

C++11

用法

restclient-cpp提供了两种与REST端点交互的方式.
有一个简单的,不需要你配置,所以如果你需要的不仅仅是一个简单的HTTP调用,
你可能会想查看高级用法。还提供了一个编码转换类, textconv_helper
使用方法:

//response utf8
std::wstring body = textconv_helper::A2W_(response.body.c_str(), CP_UTF8);
wprintf(L"body = %s \n", body.c_str());

默认使用的是Winnet Api,切换WinHttp Api

#define _USE_WIHTTP_INTERFACE

简单使用

#include "restclient-cpp/restclient.h"

RestClient::Response r = RestClient::get("http://url.com")
RestClient::Response r = RestClient::post("http://url.com/post", "text/json", "{\"foo\": \"bla\"}")
RestClient::Response r = RestClient::put("http://url.com/put", "text/json", "{\"foo\": \"bla\"}")
RestClient::Response r = RestClient::del("http://url.com/delete")
RestClient::Response r = RestClient::head("http://url.com")

响应类型为RestClient :: Response 属性:

RestClient::Response.code // HTTP response code
RestClient::Response.body // HTTP response body
RestClient::Response.headers // HTTP response headers,The type is map
RestClient::Response.cookies // HTTP response cookies,The type is string
RestClient::Response.cookie // HTTP response cookies,The type is map
RestClient::Response.get_header //It can be used to get the specified header value
RestClient::Response.get_cookie //It can be used to get the specified cookie value

高级用法

GET simple

RestClient::Request request;

request.timeout     	        = 3000;
request.followRedirects     	= false;
request.headers["User-Agent"]	= "Mozilla/5.0";
request.headers["Cookies"]	  = "name=value;";

RestClient::Response response	= RestClient::get("http://www.baidu.com", &request);

printf("%s \n", response.body.c_str());
printf("%d \n", response.code);
printf("%s \n", response.cookies.c_str());
printf("%s %s \n", response.Cookie["BAIDUID"].c_str(), response.get_cookie("BAIDUID").c_str());
printf("%s %s \n", response.headers["Location"].c_str(), response.get_header("Location").c_str());
printf("%s %s \n", response.headers["Content-Type"].c_str(), response.get_header("Content-Type").c_str());
printf("%s %s \n", response.headers["Content-Length"].c_str(), response.get_header("Content-Length").c_str());

POST simple

RestClient::Request request;

request.timeout	= 3000;
request.followRedirects	= false;
request.headers["User-Agent"]	= "Mozilla/5.0";
request.headers["Cookies"] = "name=value;";

RestClient::Response response = RestClient::post("http://www.baidu.com/post.php",
"text/json", "{\"foo\": \"bla\"}",&request);

printf("%s \n", response.body.c_str());
printf("%d \n", response.code);
printf("%s \n", response.cookies.c_str());
printf("%s \n", response.headers["Content-Type"].c_str());
printf("%s \n", response.headers["Content-Length"].c_str());

Proxy simple

RestClient::Request request;

request.proxy.proxy = "http=115.29.2.139:80";
request.proxy.username = "";
request.proxy.password = "";

//request.basicAuth.username = "your username";
//request.basicAuth.password = "your password";

request.timeout	= 3000;
request.followRedirects	= false;
request.headers["User-Agent"]	= "Mozilla/5.0";
request.headers["Cookie"] = "name=value;";

RestClient::Response response	= RestClient::get("http://ipip.yy.com/get_ip_info.php", &request);

//response utf8
std::wstring body = textconv_helper::A2W_(response.body.c_str(), CP_UTF8);
wprintf(L"body = %s \n", body.c_str());

printf("code = %d \n", response.code);
printf("cookie = %s \n", response.cookies.c_str());
printf("%s %s \n", response.Cookie["BAIDUID"].c_str(), response.get_cookie("BAIDUID").c_str());
printf("%s %s \n", response.headers["Location"].c_str(), response.get_header("Location").c_str());
printf("%s %s \n", response.headers["Content-Type"].c_str(), response.get_header("Content-Type").c_str());
printf("%s %s \n", response.headers["Content-Length"].c_str(), response.get_header("Content-Length").c_str());

TODO

  1. upload file

restclient-cpp's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.