DynuIPRefresher/API.cpp

88 lines
2.2 KiB
C++
Raw Normal View History

2019-05-01 09:14:52 +00:00
//
// Created by lukas on 06.04.19.
//
#include "API.h"
#include <string>
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include "Hashmap.h"
std::string API::request(std::string myurl) {
Hashmap<std::string,std::string> map;
std::vector<std::string> str;
return request(myurl,false,map,str);
}
std::string API::request(std::string myurl, bool post, Hashmap<std::string,std::string> &map, std::vector<std::string> headers) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
struct curl_slist *list = NULL;
for (int j = 0; j < headers.size(); ++j) {
list = curl_slist_append(list, headers.at(j).c_str());
}
// list = curl_slist_append(list, "Accept:");
std::string readString;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, myurl.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
//ssl verification
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
if(post){
std::cout << "add post data\n";
std::string poststring;
for (int i = 0; i < map.size(); i++) {
poststring+=map.getKey(i)+"="+map.getValue(i);
if(i < map.size()-1){
poststring+="&";
}
}
std::cout << "post string: " << poststring << "\n";
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, poststring.c_str());
}else{
std::string getstring;
for(int i =0; i< map.size();i++){
getstring+=map.getKey(i)+"="+map.getValue(i);
if(i < map.size()-1){
getstring+="&";
}
}
myurl+="?"+getstring;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readString);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return readString;
}
size_t API::write_data(void *contents, size_t size, size_t nmemb, FILE *stream) {
((std::string*)stream)->append((char*)contents, size * nmemb);
return size * nmemb;
}