-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_post_create_book.cpp
More file actions
78 lines (58 loc) · 2.32 KB
/
test_post_create_book.cpp
File metadata and controls
78 lines (58 loc) · 2.32 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <curl/curl.h>
#include "proto_generated/book_api.pb.h"
int main() {
GOOGLE_PROTOBUF_VERIFY_VERSION;
lists::book::api::CreateBookRequest req;
req.set_title("The C++ Programming Language");
req.set_cover_image_url("https://example.com/cpp-book.jpg");
req.set_author("Bjarne Stroustrup");
google::protobuf::Timestamp* start = req.mutable_start_date();
start->set_seconds(1700000000);
start->set_nanos(0);
google::protobuf::Timestamp* end = req.mutable_end_date();
end->set_seconds(1700864000);
end->set_nanos(0);
req.set_rating(lists::book::RATING_5);
req.set_note("Test create book via HTTP POST");
std::string payload;
if (!req.SerializeToString(&payload)) {
std::cerr << "Failed to serialize CreateBookRequest" << std::endl;
return 1;
}
std::cout << "Serialized CreateBookRequest size: " << payload.size() << " bytes\n";
CURLcode global_init_res = curl_global_init(CURL_GLOBAL_DEFAULT);
if (global_init_res != CURLE_OK) {
std::cerr << "curl_global_init() failed: "
<< curl_easy_strerror(global_init_res) << std::endl;
return 1;
}
CURL* curl = curl_easy_init();
if (!curl) {
std::cerr << "curl_easy_init() failed" << std::endl;
curl_global_cleanup();
return 1;
}
struct curl_slist* headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/x-protobuf");
headers = curl_slist_append(headers, "Accept: application/x-protobuf");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8080/api/books");
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, payload.data());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast<long>(payload.size()));
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: "
<< curl_easy_strerror(res) << std::endl;
} else {
std::cout << "\nPOST /api/books success\n";
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_global_cleanup();
google::protobuf::ShutdownProtobufLibrary();
return 0;
}