-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_http_get_post.cpp
More file actions
116 lines (101 loc) · 3.83 KB
/
2_http_get_post.cpp
File metadata and controls
116 lines (101 loc) · 3.83 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
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
105
106
107
108
109
110
111
112
113
114
115
116
// Example of using libcurl for HTTP GET and POST requests in C++
// This program demonstrates how to perform GET and POST requests using the
// libcurl library. For more detailed information on using libcurl, visit the
// official tutorial: https://curl.se/libcurl/c/libcurl-tutorial.html
#include <iostream>
#include <string>
#include <curl/curl.h>
/**
* Callback function for writing received data to a string.
* @param receivedData Buffer containing received data.
* @param dataSize Size of each data block received.
* @param dataBlocks Number of data blocks received.
* @param outputBuffer Pointer to a string where the received data will be stored.
* @return The total number of bytes taken care of. If this number differs from
* the number of bytes provided, it'll signal an error to the library.
*
* The prototype for this callback function is described here:
* https://curl.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
*/
static size_t
WriteCallback(
char *receivedData,
size_t dataSize,
size_t dataBlocks,
void *outputBuffer
) {
std::string *strBuffer = static_cast<std::string*>(outputBuffer);
strBuffer->append(receivedData, dataSize * dataBlocks);
return dataSize * dataBlocks;
}
/**
* Performs an HTTP GET request.
* @param requestUrl URL to which the GET request is made.
*/
void
performGetRequest(
const std::string& requestUrl
) {
CURL *curlHandle = curl_easy_init();
if (curlHandle) {
CURLcode result;
std::string responseBuffer;
curl_easy_setopt(curlHandle, CURLOPT_URL, requestUrl.c_str());
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &responseBuffer);
curl_easy_setopt(curlHandle, CURLOPT_HTTPGET, 1L);
result = curl_easy_perform(curlHandle);
if (result != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: "
<< curl_easy_strerror(result) << std::endl;
} else {
std::cout << "GET Request Output: " << responseBuffer << std::endl;
}
curl_easy_cleanup(curlHandle);
}
}
/**
* Performs an HTTP POST request.
* @param requestUrl URL to which the POST request is made.
* @param postData Data to be sent in the POST request.
*/
void
performPostRequest(
const std::string& requestUrl,
const std::string& postData
) {
CURL *curlHandle = curl_easy_init();
if (curlHandle) {
CURLcode result;
std::string responseBuffer;
curl_easy_setopt(curlHandle, CURLOPT_URL, requestUrl.c_str());
curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDS, postData.c_str());
curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA, &responseBuffer);
curl_easy_setopt(curlHandle, CURLOPT_POST, 1L);
result = curl_easy_perform(curlHandle);
if (result != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: "
<< curl_easy_strerror(result) << std::endl;
} else {
std::cout << "POST Request Output: " << responseBuffer << std::endl;
}
curl_easy_cleanup(curlHandle);
}
}
int
main() {
CURLcode globalInitResult = curl_global_init(CURL_GLOBAL_DEFAULT);
if (globalInitResult != CURLE_OK) {
std::cerr << "curl_global_init() failed: "
<< curl_easy_strerror(globalInitResult) << std::endl;
return 1;
}
std::string getUrl = "http://httpbin.org/get";
std::string postUrl = "http://httpbin.org/post";
std::string postData = "field1=value1&field2=value2";
performGetRequest(getUrl);
performPostRequest(postUrl, postData);
curl_global_cleanup();
return 0;
}