-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
104 lines (101 loc) · 3.35 KB
/
main.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "main.h"
int main() {
cout << registerUser();
cout << loginUser()<<std::endl;
loginToken();
cout << showAllUser()<<std::endl;
cout << showUser()<<std::endl;
cout << updateUser()<<std::endl;
cout << deleteUser()<<std::endl;
return 0;
}
size_t WriteCallback(void *contents, size_t size, size_t nmemb,
std::string *userp) {
userp->append((char *)contents, size * nmemb);
return size * nmemb;
}
std::string sendRequest(const std::string &method = "", const std::string &url="",
const std::string &postData = "",
const std::string &token = "") {
CURL *curl;
CURLcode res;
std::string readBuffer;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
struct curl_slist *headers = NULL;
headers = curl_slist_append(
headers, "Content-Type: application/json"); // Add this line
if (!token.empty()) {
std::string authHeader = "Authorization: Bearer " + token;
headers = curl_slist_append(headers, authHeader.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
if (!postData.empty()) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, method.c_str());
res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return readBuffer;
}
const string registerUser() {
nlohmann::json registerData;
registerData["name"] = "yuniar";
registerData["username"] = "yuniar";
registerData["email"] = "yuniar@gmail.com";
registerData["password"] = "password";
registerData["password_confirmation"] = "password";
nlohmann::json send_POST;
send_POST["user"] = registerData;
auto result =
sendRequest("POST", "http://localhost:3000/users", send_POST.dump());
return result;
}
const string loginUser() {
nlohmann::json loginData;
loginData["email"] = "yuniar@gmail.com";
loginData["password"] = "password";
auto result =
sendRequest("POST", "http://localhost:3000/auth/login", loginData.dump());
return result;
}
const string showAllUser() {
auto all_user =
sendRequest("GET", "http://localhost:3000/users", "", loginToken());
return all_user;
}
const string showUser() {
auto user_result =
sendRequest("GET", "http://localhost:3000/users/yuniar", "", loginToken());
return user_result;
}
const string loginToken() {
auto login_token = nlohmann::json::parse(loginUser());
auto token = login_token["token"];
return token;
}
const string updateUser() {
nlohmann::json updateData;
updateData["name"] = "yuniar gultom";
updateData["password"] = "password";
updateData["email"] = "yuniar@gmail.com";
nlohmann::json send_PATCH;
send_PATCH["user"] = updateData;
auto update_result =
sendRequest("PATCH", "http://localhost:3000/users/yuniar",
send_PATCH.dump(), loginToken());
return update_result;
}
const string deleteUser(){
auto delete_result = sendRequest("DELETE", "http://localhost:3000/users/yuniar", "", loginToken());
return delete_result;
}