How to Convert Markdown to HTML using C/C++
Markdown is designed to make web content easier to write and read within web-based applications. Our applications still need a way to convert Markdown to HTML, however. Thankfully, the below code makes that part easy.
We can use the below C/C++ examples to structure an API call that quickly converts Markdown text content to HTML, and we can use this service for free with a free-tier API key (up to 800 API calls per month).
We’ll just need to first install libcurl in our C/C++ project:
libcurl/7.75.0
And after that we can structure our request like so, copying our API key into the authorization header:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/convert/web/md/to/html");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);
Just like that, we’re all done — no more code required.