Problem getting all Droplets

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

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 = "";
  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);
  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 :-)

Submit an answer

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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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, "");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);

    std::cout << readBuffer << std::endl;
  return 0;