A libcurl C++ HTTP Request wrapper to make life easier.
HTTP requests shouldn't be hard. While C++ does not have a native library, there are several other options but they all overcomplicate the process. I was looking for a simple, liteweight solution to make calls against APIs and return the response content.
Barbeque is based on libcurl, the C++ HTTP request library, and wraps it in an easy to use fashion. Each method returns a string (of you content) which you can then use against your favorite JSON parser.
You must first install libcurl
as it is the basis for Barbeque.
Go here: http://curl.haxx.se/download.html to download the latest source archive.
Then:
- Expand the tarball
tar -xvf curl-7.XX.XX.tar
cd
into the newly created directorycurl-7.XX.XX
Run:
./configure
make
make test
sudo make install
Assuming everything went OK, libcurl should now be installed.
Now:
git clone https://github.com/g12mcgov/Barbeque.git
Move the cloned files into your local directory and finally:
#include "barbeque.h"
You must now link against libcurl and use C++ 11 when you compile:
$ g++ main.cpp barbeque.cpp -lcurl -std=c++0x -o barbeque
Currently Barbeque supports GET
and POST
operations. To do the following please look at the documentation below:
int main()
{
string url = "http://api.aerisapi.com/observations/20015/";
// Create Barbeque object
Barbeque* bbq = new Barbeque();
// Pass in our URL
string response1 = bbq->get(url);
}
Simply declare the Barbeque object (I strongly suggest doing so dynamically), and use it as shown in the example. The GET
method expects a string to be passed.
The POST
method expects a <map>
to be passed in as the argument. This way, you can construct Key/Value pairs for APIs. For instance, the example below shows a scenario where the given API requires 2 parameters.
client_id
client_secret
#include <map>
...
int main()
{
string url = "http://api.aerisapi.com/observations/20015/";
// Construct a map for our JSON params. Much like a Python Dict
map<string, string> params;
// Populate the map
params["client_id"] = "#######";
params["client_secret"] = "########";
// Construct our [optional] headers map
map<string, string> headers;
// Populate the map
headers["Content-type"] = "application/json";
// Declare a new Barbeque object
Barbeque* bbq = new Barbeque();
// Here we call it with no headers
string response1 = bbq->post(url, params);
// Optionally (and alternatively), we can add headers.
string response2 = bbq->post(url, params, headers);
}
Simply declare the Barbeque object (I strongly suggest doing so dynamically), and use it as shown in the example. The POST
method has been overloaded to also accept headers.
Happy HTTP requesting!