-
Notifications
You must be signed in to change notification settings - Fork 30
Recipe: Downloading and Posting data
Himanshu Shekhar edited this page Oct 4, 2017
·
4 revisions
HTTP GET a url say, https://github.com/sony/easyhttpcpp in a blocking manner and print the response status, header and the body as a string.
In case of large files, avoid using toString() method on the response body. Use getByteStream() to read data as a stream.
try {
// create a default http client instance
easyhttpcpp::EasyHttp::Ptr pHttpClient = easyhttpcpp::EasyHttp::Builder().build();
// create a new request and execute synchronously
easyhttpcpp::Request::Builder requestBuilder;
easyhttpcpp::Request::Ptr pRequest = requestBuilder.setUrl("https://github.com/sony/easyhttpcpp").build();
easyhttpcpp::Call::Ptr pCall = pHttpClient->newCall(pRequest);
easyhttpcpp::Response::Ptr pResponse = pCall->execute();
if (!pResponse->isSuccessful()) {
std::cout << "HTTP GET Error: (" << pResponse->getCode() << ")" << std::endl;
} else {
std::cout << "HTTP GET Success!" << std::endl;
}
// print status
std::cout << "Http status code: " << pResponse->getCode() << std::endl;
std::cout << "Http status message: " << pResponse->getMessage() << std::endl;
// print headers
std::cout << "Http response headers:\n" << pResponse->getHeaders()->toString() << std::endl;
// print body
std::cout << "Http response body:\n" << pResponse->getBody()->toString() << std::endl;
} catch (const std::exception& e) {
// maybe network error
std::cout << "Error occurred: " << e.what() << std::endl;
}
HTTP POST a string to a server in a blocking manner and print the response status, header and the body as a string.
In case of bigger data or files, it is recommended to post data as a stream.
try {
// create a default http client instance
easyhttpcpp::EasyHttp::Ptr pHttpClient = easyhttpcpp::EasyHttp::Builder().build();
// this becomes the Content-Type of the request
easyhttpcpp::MediaType::Ptr pMediaType(new easyhttpcpp::MediaType("text/x-markdown; charset=utf-8"));
// prep request body to post
std::string requestBodyStr = "Hello World!";
easyhttpcpp::RequestBody::Ptr pRequestBody = easyhttpcpp::RequestBody::create(pMediaType, requestBodyStr);
// create a new request and execute synchronously
easyhttpcpp::Request::Builder requestBuilder;
easyhttpcpp::Request::Ptr pRequest = requestBuilder.setUrl("https://api.github.com/markdown/raw")
.httpPost(pRequestBody).build();
easyhttpcpp::Call::Ptr pCall = pHttpClient->newCall(pRequest);
easyhttpcpp::Response::Ptr pResponse = pCall->execute();
if (!pResponse->isSuccessful()) {
std::cout << "HTTP GET Error: (" << pResponse->getCode() << ")" << std::endl;
} else {
std::cout << "HTTP GET Success!" << std::endl;
}
// print status
std::cout << "Http status code: " << pResponse->getCode() << std::endl;
std::cout << "Http status message: " << pResponse->getMessage() << std::endl;
// print headers
std::cout << "Http response headers:\n" << pResponse->getHeaders()->toString() << std::endl;
// print body
std::cout << "Http response body:\n" << pResponse->getBody()->toString() << std::endl;
} catch (const std::exception& e) {
// maybe network error
std::cout << "Error occurred: " << e.what() << std::endl;
}
Feel free to open new issue if you want to ask any questions.