nicehash-api-cpp  0.0.1
get.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <curl/curl.h>
3 
4 #include "../../include/client.hpp"
5 
6 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp);
7 
8 /**
9  * \brief GET request the provided url and return the response body as a string
10  *
11  * @param url the URL to GET request
12  * @return The response body
13  */
14 std::string Client::get (const std::string url) {
15  CURL *curl = curl_easy_init();
16  CURLcode res;
17  std::string readBuffer;
18 
19  if(curl) {
20  // We need to convert the string url to a char* here
21  // see https://stackoverflow.com/a/39693179
22  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
23  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
24  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
25 
26  // Error codes can be found at
27  // https://curl.haxx.se/libcurl/c/libcurl-errors.html
28  res = curl_easy_perform(curl);
29  curl_easy_cleanup(curl);
30  }
31 
32  return readBuffer;
33 }
34 
35 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
36 {
37  ((std::string*)userp)->append((char*)contents, size * nmemb);
38  return size * nmemb;
39 }
virtual std::string get(std::string const url)
GET request the provided url and return the response body as a string.
Definition: get.cpp:14
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
Definition: get.cpp:35