-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_simplest_http_get.cpp
More file actions
37 lines (31 loc) · 965 Bytes
/
1_simplest_http_get.cpp
File metadata and controls
37 lines (31 loc) · 965 Bytes
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
#include <curl/curl.h>
#include <iostream>
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;
}
int
main() {
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
std::string readBuffer;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
std::cerr << "Error: " << curl_easy_strerror(res) << std::endl;
curl_easy_cleanup(curl);
}
curl_global_cleanup();
std::cout << "Data: " << readBuffer << std::endl;
return 0;
}