CMS 3D CMS Logo

WebUtils.cc
Go to the documentation of this file.
2 //
3 #include <curl/curl.h>
4 #include <cstdio>
5 #include <cstring>
6 
7 namespace cond {
8 
9  // callback to obtain the Get result
10  static size_t getBodyCallback(void* contents, size_t size, size_t nmemb, void* ptr) {
11  // Cast ptr to std::string pointer and append contents to that string
12  ((std::string*)ptr)->append((char*)contents, size * nmemb);
13  return size * nmemb;
14  }
15 
16  unsigned long httpGet(const std::string& urlString, std::string& info) {
17  CURL* curl;
18  CURLcode res;
20  char errbuf[CURL_ERROR_SIZE];
21 
22  curl = curl_easy_init();
23  unsigned long ret = false;
24  if (curl) {
25  struct curl_slist* chunk = nullptr;
26  chunk = curl_slist_append(chunk, "content-type:application/json");
27  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
28  curl_easy_setopt(curl, CURLOPT_URL, urlString.c_str());
29  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getBodyCallback);
30  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
31  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
32  res = curl_easy_perform(curl);
33  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &ret);
34  if (CURLE_OK == res) {
35  info = body;
36  } else {
37  size_t len = strlen(errbuf);
38  fprintf(stderr, "\nlibcurl: (%d) ", res);
39  if (len)
40  fprintf(stderr, "%s%s", errbuf, ((errbuf[len - 1] != '\n') ? "\n" : ""));
41  else
42  fprintf(stderr, "%s\n", curl_easy_strerror(res));
43  }
44  curl_easy_cleanup(curl);
45  }
46  return ret;
47  }
48 
49 } // namespace cond
static const TGPicture * info(bool iBackgroundIsBlack)
static size_t getBodyCallback(void *contents, size_t size, size_t nmemb, void *ptr)
Definition: WebUtils.cc:10
ret
prodAgent to be discontinued
Definition: Electron.h:6
body
create merge config
Definition: mps_merge.py:62
unsigned long httpGet(const std::string &urlString, std::string &info)
Definition: WebUtils.cc:16