Save Curl Content Result into a String in C++

Save cURL content result into a string in C++

You will have to use CURLOPT_WRITEFUNCTION to set a callback for writing. I can't test to compile this right now, but the function should look something close to;

static std::string readBuffer;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
readBuffer.append(contents, realsize);
return realsize;
}

Then call it by doing;

readBuffer.clear();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
// ...other curl options
res = curl_easy_perform(curl);

After the call, readBuffershould have your contents.

Edit: You can use CURLOPT_WRITEDATA to pass the buffer string instead of making it static. In this case I just made it static for simplicity. A good page to look (besides the linked example above) is here for an explanation of the options.

Edit2: As requested, here's a complete working example without the static string buffer;

#include <iostream>
#include <string>
#include <curl/curl.h>

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

int main(void)
{
CURL *curl;
CURLcode res;
std::string readBuffer;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

std::cout << readBuffer << std::endl;
}
return 0;
}

C libcurl get output into a string

You can set a callback function to receive incoming data chunks using curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, myfunc);

The callback will take a user defined argument that you can set using curl_easy_setopt(curl, CURLOPT_WRITEDATA, p)

Here's a snippet of code that passes a buffer struct string {*ptr; len} to the callback function and grows that buffer on each call using realloc().

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct string {
char *ptr;
size_t len;
};

void init_string(struct string *s) {
s->len = 0;
s->ptr = malloc(s->len+1);
if (s->ptr == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
size_t new_len = s->len + size*nmemb;
s->ptr = realloc(s->ptr, new_len+1);
if (s->ptr == NULL) {
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;

return size*nmemb;
}

int main(void)
{
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if(curl) {
struct string s;
init_string(&s);

curl_easy_setopt(curl, CURLOPT_URL, "curl.haxx.se");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);

printf("%s\n", s.ptr);
free(s.ptr);

/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}

C++ - sending Curl requests gives the response in the console without printing it

By default, curl writes the received data to stdout. You can change that by using curl_easy_setopt() to specify a custom CURLOPT_WRITEFUNCTION callback, giving it a string* pointer via CURLOPT_WRITEDATA. For example:

static size_t writeToString(void *data, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
std::string *str = static_cast<std::string*>(userp);
str->append(static_cast<char*>(data), realsize);
return realsize;
}

...

CURL *curl = curl_easy_init();
if (curl) {
...
std::string respStr;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeToString);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &respStr);

CURLcode res = curl_easy_perform(curl);

// use respStr as needed...

curl_easy_cleanup(curl);
}

c Curl get request to variable

  1. You declare write_to_string to accept std::string but you pass it a pointer to std::string (via CURLOPT_WRITEDATA).
  2. You don't do anything with std::string stream in write_to_string.
  3. You declare response as a std::string* but do not initialize it, so you have dangling pointer.

Need to store result of cURL http request as a map in C++

You are grabbing a JSON (JavaScript Object Notation) file. To make your life much easier you should look into using a library for processing JSON in C++ like jsoncpp. This site here provides a quick tutorial.

save the output of a curl perform to a vectorstring in c++

I don't know curl, so I'm going to assume the setup code is correct. So what you want is the callback function to add a string for each block of data received to a vector of strings. This also assumes that the data coming back is 8-bit characters.

vector<string> contents;

size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
contents.push_back(string(static_cast<const char*>(ptr), size * nmemb));
return size * nmemb;
}

the "call" to string() actually constructs a string object initialized with a pointer and data length.



Related Topics



Leave a reply



Submit