I'm writing a class to create a simple http get request using libcurl2. I get back the following error:
Libcurl error Failed writing received data to disk/application
I'm fairly new to C++, still learning but i'm guessing it has something to do with the Access Modifiers or the scope
I hope someone can help me out, with this.
class HTTPconnection
#include "HTTPconnection.h"
#include <iostream>
#include <string>
using namespace httptest;
HTTPconnection::HTTPconnection()
{
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
}
size_t HTTPconnection::write_callback(char * data, size_t
size, size_t nmemb, std::string * writerData)
{
if (writerData == NULL)
return 0;
writerData->append(data, size*nmemb);
return size * nmemb;
}
std::string HTTPconnection::createConnection(const char *url, const char *proxy)
{
curl_easy_setopt(HTTPconnection::curl, CURLOPT_URL, url);
curl_easy_setopt(HTTPconnection::curl, CURLOPT_WRITEFUNCTION, &HTTPconnection::write_callback);
curl_easy_setopt(HTTPconnection::curl, CURLOPT_WRITEDATA, &HTTPconnection::buffer);
res = curl_easy_perform(HTTPconnection::curl);
std::cout << curl_easy_strerror(res);
return buffer;
}
HTTPconnection::~HTTPconnection()
{
curl_easy_cleanup(curl);
curl_global_cleanup();
}
HTTPconnection.h
#ifndef HTTPconnection_H
#define HTTPconnection_H
#include <string>
#include "libcurl/include/curl/curl.h"
#ifdef _DEBUG
#pragma comment(lib, "libcurl/lib/libcurl_a_debug.lib")
#else
#pragma comment(lib, "libcurl/lib/libcurl_a.lib")
#endif
namespace httptest
{
class HTTPconnection
{
public:
//Default Constructor
HTTPconnection();
std::string createConnection(const char *url, const char *proxy);
//Destructor
~HTTPconnection();
private:
//methods
size_t write_callback(char *data, size_t size, size_t nmemb, std::string *writerData);
//members
std::string buffer;
CURL *curl;
CURLcode res;
};
}
#endif // !HTTPconnection_H
The error you get from the call to curllib API indicates that problem occurred during saving the received data.
Since you've set the callback curl thinks the callback has failed.
Curl will suppose that callback failed to process received data is the returned value is different from the number of bytes received.
In your case it means you've entered the branch with return 0;
- you should be able to debug it by setting breakpoint in your callback.
The reason for nullptr received in the last parameter of the callback - you have declared it as non-static member of the class which means it has hidden first parameter this which C code calling this callback has no knowledge about.
The solution in your case should be easy - declare the write_callback as static (or free) function. Set breakpoint inside it and see that all parameters provided by curl are valid. You may be required to change the callback calling conventions to match the function signature expected by the curllib.
Callback function needs to return size_t object:
static size_t write_data(char *ptr, size_t size, size_t nmemb, void *user){
// If you're expecting JSON as response: parse pointer
nlohmann::json res = nlohmann::json::parse(ptr);
// print JSON
std::cout << res.dump();
return size * nmemb;
}
More info here and here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With