Support chunked response content (but no Trailer headers).
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
set(LIBS webcc jsoncpp ${Boost_LIBRARIES} "${CMAKE_THREAD_LIBS_INIT}")
|
||||
|
||||
set(LIBS ${LIBS} ${OPENSSL_LIBRARIES})
|
||||
if(WIN32)
|
||||
set(LIBS ${LIBS} crypt32)
|
||||
endif()
|
||||
|
||||
if(UNIX)
|
||||
# Add `-ldl` for Linux to avoid "undefined reference to `dlopen'".
|
||||
set(LIBS ${LIBS} ${CMAKE_DL_LIBS})
|
||||
endif()
|
||||
|
||||
add_executable(github_rest_client main.cc)
|
||||
|
||||
target_link_libraries(github_rest_client ${LIBS})
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "json/json.h"
|
||||
|
||||
#include "webcc/rest_ssl_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
static Json::Value StringToJson(const std::string& str) {
|
||||
Json::Value json;
|
||||
|
||||
Json::CharReaderBuilder builder;
|
||||
std::stringstream stream(str);
|
||||
std::string errs;
|
||||
if (!Json::parseFromStream(builder, stream, &json, &errs)) {
|
||||
std::cerr << errs << std::endl;
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
void Test() {
|
||||
webcc::RestSslClient client("api.github.com");
|
||||
|
||||
if (client.Get("/events")) {
|
||||
Json::Value json = StringToJson(client.response()->content());
|
||||
|
||||
// Pretty print the JSON.
|
||||
|
||||
Json::StreamWriterBuilder builder;
|
||||
builder["indentation"] = " ";
|
||||
|
||||
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
|
||||
writer->write(json, &std::cout);
|
||||
|
||||
std::cout << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(client.error());
|
||||
if (client.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
Test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user