Working on..

dev
mbandic 11 months ago
parent ad12c4c1c1
commit dfda4cb292
  1. 2
      .gitignore
  2. 2
      LICENSE
  3. 1
      dependency
  4. 30
      lib/cppurl.hpp
  5. 30
      src/cppurl.cpp
  6. 1
      test/compile.sh
  7. BIN
      test/test
  8. 17
      test/test.cpp

2
.gitignore vendored

@ -0,0 +1,2 @@
.vscode
example

@ -58,7 +58,7 @@ APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Copyright [2023] [Marcel Bandić - marcelb96@yahoo.com]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

@ -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

Binary file not shown.

@ -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…
Cancel
Save