Working on..
This commit is contained in:
parent
ad12c4c1c1
commit
dfda4cb292
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.vscode
|
||||||
|
example
|
2
LICENSE
2
LICENSE
@ -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.
|
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");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
1
dependency
Normal file
1
dependency
Normal file
@ -0,0 +1 @@
|
|||||||
|
libcurl4-openssl-dev
|
30
lib/cppurl.hpp
Normal file
30
lib/cppurl.hpp
Normal file
@ -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
|
30
src/cppurl.cpp
Normal file
30
src/cppurl.cpp
Normal file
@ -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;
|
||||||
|
}
|
1
test/compile.sh
Normal file
1
test/compile.sh
Normal file
@ -0,0 +1 @@
|
|||||||
|
g++ test.cpp ../src/*.cpp -o test -lcurl
|
17
test/test.cpp
Normal file
17
test/test.cpp
Normal file
@ -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…
x
Reference in New Issue
Block a user