forked from WiiLink24/AccountLinker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.cpp
More file actions
54 lines (45 loc) · 1.54 KB
/
http.cpp
File metadata and controls
54 lines (45 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "http.h"
#include <sys/unistd.h>
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* output) {
size_t total_size = size * nmemb;
output->append(static_cast<char *>(contents), total_size);
return total_size;
}
HTTPResponse http_post(std::string_view url, std::string_view post_data, const std::vector<std::string>& headers) {
CURL* curl = curl_easy_init();
CURLcode res{};
std::string response{};
long http_code{};
if (curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, url.data());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.data());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_slist* curl_headers{};
for (const auto& header : headers) {
curl_headers = curl_slist_append(curl_headers, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_headers);
res = curl_easy_perform(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_slist_free_all(curl_headers);
curl_easy_cleanup(curl);
}
// Attempt to parse JSON
json j{};
std::string json_error{};
try {
j = json::parse(response);
} catch (std::exception& e) {
json_error = e.what();
}
return HTTPResponse{
.j = j,
.json_error = json_error,
.status_code = http_code,
.curl_code = res,
.resp = response,
};
}