Using FreshBooks API from C++
Despite all the new modern languages that are in wide use today, C++ is still my favorite programming language. But, I must admit, it is not always clear how to do some things that may seem trivial in C# or other languages with a huge framework behind.
I assembled a small example that can be used as a starting point for a more complex project that uses the FreshBooks API from C++. You’ll need to have libcurl downloaded and known to your compiler. In the attachment is a very small C++ class that is a wrapper around the C functions from the library.
The program is requesting for the list of clients for a FreshBooks account. Here is a snippet from the code:
/*****************************************************************/
int main(int argc, char* argv[])
{
std::stringstream strReq;
strReq << “<?xml version=\”1.0\” encoding=\”utf-8\”?>”
<< “<request method=\”client.list\”>”
<< “</request>”;
curl::Http ht;
ht.option(CURLAUTH_BASIC, CURLOPT_HTTPAUTH);
ht.option(CURLOPT_USERPWD, “YOUR-TOKEN-HERE”);
ht.option(CURLOPT_URL, “https://YOUR-ACCOUNT-NAME.freshbooks.com/api/2.1/xml-in”);
ht.option(CURLOPT_SSL_VERIFYPEER, 0);
ht.option(CURLOPT_SSL_VERIFYHOST, 2);
ht.option(CURLOPT_PORT, 443);
ht.option(CURLOPT_POSTFIELDS, strReq.str().c_str());
ht.execute();
const std::vector
std::cout << &data[0] << std::endl;
return 0;
}
/*****************************************************************/
You’ll have to link with libcurl_imp.lib. You should change the connection info with a valid token and account name. I believe that’s it, the program should print the list of clients in the console window.
