parent
ad12c4c1c1
commit
dfda4cb292
@ -0,0 +1,2 @@ |
||||
.vscode |
||||
example |
@ -0,0 +1 @@ |
||||
libcurl4-openssl-dev |
@ -0,0 +1,30 @@ |
||||
#ifndef _CPPURL_ |
||||
#define _CPPURL_ |
||||
|
||||
#include <curl/curl.h> |
||||
#include <string> |
||||
|
||||
namespace marcelb { |
||||
|
||||
using namespace std; |
||||
|
||||
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp);
|
||||
|
||||
class Curl { |
||||
CURL *curl; |
||||
CURLcode res; |
||||
string readBuffer; |
||||
|
||||
|
||||
|
||||
public: |
||||
// Curl();
|
||||
string request(const string& req); |
||||
|
||||
|
||||
}; |
||||
|
||||
|
||||
} |
||||
|
||||
#endif |
@ -0,0 +1,30 @@ |
||||
|
||||
#include "../lib/cppurl.hpp" |
||||
|
||||
using namespace marcelb; |
||||
|
||||
// marcelb::Curl::Curl() {
|
||||
// }
|
||||
|
||||
|
||||
string marcelb::Curl::request(const string& req){ |
||||
curl = curl_easy_init(); |
||||
|
||||
readBuffer.clear(); |
||||
|
||||
if(curl) { |
||||
curl_easy_setopt(curl, CURLOPT_URL, req.c_str()); |
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); |
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); |
||||
res = curl_easy_perform(curl); |
||||
curl_easy_cleanup(curl); |
||||
} |
||||
|
||||
return readBuffer; |
||||
} |
||||
|
||||
|
||||
static size_t marcelb::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { |
||||
((std::string*)userp)->append((char*)contents, size * nmemb); |
||||
return size * nmemb; |
||||
} |
@ -0,0 +1 @@ |
||||
g++ test.cpp ../src/*.cpp -o test -lcurl |
@ -0,0 +1,17 @@ |
||||
#include <iostream> |
||||
|
||||
#include "../lib/cppurl.hpp" |
||||
|
||||
using namespace std; |
||||
using namespace marcelb; |
||||
|
||||
int main () { |
||||
|
||||
Curl rest; |
||||
|
||||
cout << rest.request("https://reqres.in/api/unknown/2") << endl; |
||||
|
||||
cout << rest.request("https://reqres.in/api/users/2") << endl; |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue