Problem getting all Droplets

November 17, 2014 912 views

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 :-)

1 Answer

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;
}
Have another answer? Share your knowledge.