By karimcitoh
Hello DigitalOcean community :D
First of all I would like to thank you in advance for your time reading this and also for your help.
I am implementing a program who needs to make operations with DigitalOcean virtual machines, and it works most of what I have implemented.
The only thing that doesn’t work is when I try to get information about all droplets, which should be one of the easiest functions :S https://developers.digitalocean.com/#list-all-droplets
The problem with this function is that when it performs the curl request, after a moment it just breaks and the application stops running, it doesn’t return any value. My function to perform this request is the next one: (in c++)
void* curl = curl_easy_init();
std::string url = "https://api.digitalocean.com/v2/droplets/";
std::cout << "url=" << url << std::endl;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "deflate");
struct curl_slist *slist = NULL;
std::string authHeader = "Authorization: Bearer " + secretToken;
slist = curl_slist_append(slist, authHeader.c_str());
curl_slist_append(slist, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
std::stringstream out;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);
cout << "This message is printed" << endl;
CURLcode res = curl_easy_perform(curl);
//THE PROGRAM STOPS HERE
cout << "This message is never printed" << endl;
/* Check for errors */
if (res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
return out.str();
I would appreciate a lot if somebody could see where is my mistake, since I have been a long time looking for it and I can’t see it. Or maybe I can get an example of getting all droplets, which I didn’t find while researching the community questions.
Thanks a lot in advance :-)
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Looks like you need to set a callback for writing with CURLOPT_WRITEFUNCTION Here’s an example of using the API via C++ to list all active droplets:
#include <iostream>
#include <string>
#include <curl/curl.h>
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
struct curl_slist *slist = NULL;
std::string authHeader = "Authorization: Bearer XXXXXXXXXXXXXXX";
slist = curl_slist_append(slist, authHeader.c_str());
curl_slist_append(slist, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(curl, CURLOPT_URL, "https://api.digitalocean.com/v2/droplets/");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
std::cout << readBuffer << std::endl;
}
return 0;
}
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.