Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/josephp91/curlcpp

An object oriented C++ wrapper for CURL (libcurl)
https://github.com/josephp91/curlcpp

c-plus-plus cpp cross-platform curl lcurlcpp-lcurl libcurl network-requests networking receiver

Last synced: 5 days ago
JSON representation

An object oriented C++ wrapper for CURL (libcurl)

Awesome Lists containing this project

README

        

curlcpp
=======

An object-oriented C++ wrapper for cURL tool

If you want to know a bit more about cURL and libcurl, you should go on the official website http://curl.haxx.se/

Donate
======

Help me to improve this project!

Donate button

Compile and link manually
=========================

Standalone (static library)
----------

```bash
cd build
cmake ..
make
```

Standalone (dynamic/shared library)
----------

```bash
cd build
cmake .. -DBUILD_SHARED_LIBS=SHARED
make
```

**Note:** cURL >= 7.34.0 is required.

When linking curlcpp to your application don't forget to also link `curl`. Example:

```bash
g++ -std=c++11 example.cpp -I/usr/local/include/curlcpp/ -lcurlcpp -lcurl
```

Submodule
---------

When using a git submodule and CMake-buildsystem, add the following lines to your `CMakeLists.txt`:

```
ADD_SUBDIRECTORY(ext/curlcpp) # Change `ext/curlcpp` to a directory according to your setup
INCLUDE_DIRECTORIES(${CURLCPP_SOURCE_DIR}/include)
```

Install via Homebrew
====================

curlcpp is now available also via homebrew package manager:

```bash
brew install curlcpp
```

Examples
========

Here are some usage examples. You will find more examples in the test folder!

Here's an example of a simple HTTP request to get google web page, using the curl_easy interface:

* ### Simple request

`````c++
#include "curlcpp/curl_easy.h"

using curl::curl_easy;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;

/**
* This example shows how to make a simple request with curl.
*/
int main() {
// Easy object to handle the connection.
curl_easy easy;

// Add some options.
easy.add("http://");
easy.add(1L);

try {
easy.perform();
} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr< ios(stream);

// Declaration of an easy object
curl_easy easy(ios);

// Add some option to the curl_easy object.
easy.add("http://");
easy.add(1L);

try {
easy.perform();

// Retrieve information about curl current session.
auto x = easy.get_info();

/**
* get_info returns a curl_easy_info object. With the get method we retrieve
* the std::pair object associated with it: the first item is the return code of the
* request. The second is the element requested by the specified libcurl macro.
*/
std::cout<

#include "curlcpp/curl_easy.h"
#include "curlcpp/curl_pair.h"
#include "curlcpp/curl_form.h"
#include "curlcpp/curl_exception.h"

using std::string;

using curl::curl_form;
using curl::curl_easy;
using curl::curl_pair;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;

int main(int argc, const char * argv[]) {
curl_form form;
curl_easy easy;

// Forms creation
curl_pair name_form(CURLFORM_COPYNAME,"user");
curl_pair name_cont(CURLFORM_COPYCONTENTS,"you username here");
curl_pair pass_form(CURLFORM_COPYNAME,"passw");
curl_pair pass_cont(CURLFORM_COPYCONTENTS,"your password here");

try {
// Form adding
form.add(name_form,name_cont);
form.add(pass_form,pass_cont);

// Add some options to our request
easy.add("http://");
easy.add(false);
easy.add(form.get());
// Execute the request.
easy.perform();

} catch (curl_easy_exception &error) {
// If you want to get the entire error stack we can do:
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
}
return 0;
}
`````

* ### Store response in a file

And if we would like to put the returned content in a file? Nothing easier than:

`````c++
#include
#include
#include

#include "curlcpp/curl_easy.h"
#include "curlcpp/curl_ios.h"
#include "curlcpp/curl_exception.h"

using std::cout;
using std::endl;
using std::ostream;
using std::ofstream;

using curl::curl_easy;
using curl::curl_ios;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;

int main(int argc, const char * argv[]) {
// Create a file
ofstream myfile;
myfile.open ("/path/to/your/file");

// Create a curl_ios object to handle the stream
curl_ios writer(myfile);
// Pass it to the easy constructor and watch the content returned in that file!
curl_easy easy(writer);

// Add some option to the easy handle
easy.add("http://");
easy.add(1L);
try {
// Execute the request
easy.perform();

} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr<
#include

#include "curlcpp/curl_easy.h"
#include "curlcpp/curl_form.h"
#include "curlcpp/curl_ios.h"
#include "curlcpp/curl_exception.h"

using std::cout;
using std::endl;
using std::ostringstream;

using curl::curl_easy;
using curl::curl_ios;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;

int main() {
// Create a stringstream object
ostringstream str;
// Create a curl_ios object, passing the stream object.
curl_ios writer(str);

// Pass the writer to the easy constructor and watch the content returned in that variable!
curl_easy easy(writer);
// Add some option to the easy handle
easy.add("http://");
easy.add(1L);

try {
easy.perform();

// Let's print the stream content
cout<
#include

#include "curlcpp/curl_easy.h"
#include "curlcpp/curl_form.h"
#include "curlcpp/curl_pair.h"
#include "curlcpp/curl_receiver.h"
#include "curlcpp/curl_exception.h"
#include "curlcpp/curl_sender.h"

using std::cout;
using std::endl;
using std::string;

using curl::curl_form;
using curl::curl_easy;
using curl::curl_sender;
using curl::curl_receiver;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;

int main(int argc, const char * argv[]) {
// Simple request
string request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
// Creation of easy object.
curl_easy easy;
try {
easy.add("http://");
// Just connect
easy.add(true);
// Execute the request.
easy.perform();

} catch (curl_easy_exception &error) {
// If you want to get the entire error stack we can do:
curlcpp_traceback errors = error.get_traceback();
// Otherwise we could print the stack like this:
error.print_traceback();
}

// Creation of a sender. You should wait here using select to check if socket is ready to send.
curl_sender sender(easy);
sender.send(request);
// Prints che sent bytes number.
cout<<"Sent bytes: "< receiver;
// Receive the content on the easy handler
receiver.receive(easy);
// Prints the received bytes number.
cout<<"Receiver bytes: "<
#include

#include "curlcpp/curl_easy.h"
#include "curlcpp/curl_multi.h"
#include "curlcpp/curl_ios.h"

using curl::curl_easy;
using curl::curl_multi;
using curl::curl_ios;
using curl::curl_easy_exception;
using curl::curlcpp_traceback;

/**
* This example shows how to make multiple requests
* using curl_multi interface.
*/
int main() {
std::vector urls;
urls.emplace_back("https://google.com");
urls.emplace_back("https://facebook.com");
urls.emplace_back("https://linkedin.com");

// Create a vector of curl easy handlers.
std::vector handlers;

// Create a vector of curl streams.
std::vector> streams;

// Create the curl easy handler and associated the streams with it.
for (const auto & url : urls) {
auto *output_stream = new std::ostringstream;
curl_ios curl_stream(*output_stream);

curl_easy easy(curl_stream);
easy.add(url.c_str());
easy.add(1L);

streams.emplace_back(curl_stream);
handlers.emplace_back(easy);
}

// Create a map of curl pointers to output streams.
std::unordered_map*> easy_streams;
for (int i = 0; i < handlers.size(); ++i) {
easy_streams[handlers.at(i).get_curl()] = (curl_ios*)&streams.at(i);
}

// Add all the handlers to the curl multi object.
curl_multi multi;
multi.add(handlers);

try {
// Start the transfers.
multi.perform();

// Until there are active transfers, call the perform() API.
while (multi.get_active_transfers()) {
multi.perform();

// Extracts the first finished request.
std::unique_ptr message = multi.get_next_finished();
if (message != nullptr) {
const curl_easy *handler = message->get_handler();

// Get the stream associated with the curl easy handler.
curl_ios stream_handler = *easy_streams[handler->get_curl()];

auto content = stream_handler.get_stream()->str();
auto url = handler->get_info();
auto response_code = handler->get_info();
auto content_type = handler->get_info();
auto http_code = handler->get_info();

std::cout << "CODE: " << response_code.get()
<< ", TYPE: " << content_type.get()
<< ", HTTP_CODE: " << http_code.get()
<< ", URL: " << url.get()
<< ", CONTENT: " << content.substr(0, 10) + " ... "
<< std::endl;
}
}

// Free the memory allocated for easy streams.
for (auto stream : streams) {
delete stream.get_stream();
}

} catch (curl_easy_exception &error) {
// If you want to print the last error.
std::cerr<