error: 'ios_base' has not been declared - c++

I am using libcurl to download serialized code and bust it open but, I get an error that looks like fstream is missing but it is very much included. I looked around but very little on the error.
Below is the error and code.
What did miss?
compile error output
g++ -g testGetprice2.cpp -o testGetprice2.o -std=gnu++11 -lcurl
testGetprice2.cpp: In function 'int getData()':
testGetprice2.cpp:45:56: error: 'ios_base' has not been declared
testGetprice2.cpp:45:72: error: 'ios_base' has not been declared
code:
#include "rapidjson/include/rapidjson/document.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <curl/curl.h>
#include <unistd.h>
#include <unordered_map>
#include <string>
using namespace rapidjson;
struct myData
{
std::fstream *file;
std::string *str;
};
size_t write_data(void *ptr, size_t size, size_t nmemb, myData *data)
{
size_t numBytes = size * nmemb;
if (data->file)
data->file->write((char*)ptr, numBytes);
if (data->str)
*data->str += std::string((char*)ptr, numBytes);
return numBytes;
}
//function to get coin data and perform analysis
int getData()
{
int count = 0;
//begin non terminating loop
while(true)
{
count++;
CURL *curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");
std::fstream file("/home/coinz/cryptsy/myfile.txt", ios_base::out | ios_base::ate);
std::string json;
myData data;
data.file = &file;
data.str = &json;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
{
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
else
{
file << std::endl;
//begin deserialization
Document document;
document.Parse(json.c_str());
assert(document.HasMember("lasttradeprice"));
assert(document["hello"].IsString());
std::cout << "The Last Traded Price is = " << document["lasttradeprice"].GetString() << std::endl;
}
/* always cleanup */
curl_easy_cleanup(curl);
}
//timer for URL request. *ADUJST ME AS DESIRED*
usleep(10000000);
}
return 0;
}
//Le Main
int main(void)
{
getData();
}

ios_base is in namespace std. Add prefix std:: before ios_base.

Related

Capturing the output from libcurl in a std::string in c++ under Windows

I want to capture the libcurl error output that is normally sent to stderr into a std::string instead.
This is the code I have come up with so far:
#include <windows.h>
#include <streambuf>
#include <iostream>
#include <io.h>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <string>
#include "curl\curl.h"
std::string data; //will hold the url's contents
// callback function for curl
size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{ //callback must have this declaration
//buf is a pointer to the data that curl has for us
//size*nmemb is the size of the buffer
for (int c = 0; c < size * nmemb; c++)
{
data.push_back(buf[c]);
}
return size * nmemb; //tell curl how many bytes we handled
}
int main()
{
CURL* curl; //our curl object
curl_global_init(CURL_GLOBAL_ALL); //pretty obvious
curl = curl_easy_init();
CURLcode res;
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_URL, "http://ww.example.com/path");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); //tell curl to output its progress
int old;
FILE* DataFile;
old = _dup(2); // redirect stderr to "old"
if (fopen_s(&DataFile, "data", "w") != 0)
{
puts("Can't open file 'Datafile'\n");
exit(1);
}
// stderr now refers to file "Datafile"
if (-1 == _dup2(_fileno(DataFile), 2))
{
perror("Can't _dup2 stderr");
exit(1);
}
res = curl_easy_perform(curl);
// Flush stderr stream buffer so it goes to correct file
fflush(stderr);
fclose(DataFile);
// Restore original stderr
_dup2(old, 2);
curl_easy_cleanup(curl);
curl_global_cleanup();
std::ifstream filetoread(DataFile);
if (filetoread)
{
// get length of file:
filetoread.seekg(0, filetoread.end);
int length = filetoread.tellg();
filetoread.seekg(0, filetoread.beg);
char* buffer = new (std::nothrow) char[length];
if (buffer == nullptr)
{
std::cout << "Error allocating buffer.\nvariable 'length' is: " << length << std::endl;
return 0;
}
std::cout << "Reading " << length << " characters... ";
// read data as a block:
filetoread.read(buffer, length);
if (filetoread)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << filetoread.gcount() << " could be read";
filetoread.close();
// ...buffer contains the entire file...
std::string buffertext(buffer);
delete[] buffer;
}
_flushall();
}
It seems to me that the file Datafile is empty since the variable length is -1. Can anybody help with this code? The examples in redirect stdout/stderr to a string haven't proved to be a solution to my question so far.
I'd use the FUNCTION/DATA pairs in a wrapper class to be able to create event handlers that are called automatically for the events you'd like to handle. Here's an example with comments in the code:
#include "curl/curl.h"
#include <iostream>
#include <memory>
#include <stdexcept>
#include <utility>
// A simple wrapper class for the C functions in libcurl
class EasyCurly {
public:
// default constructor
EasyCurly() : handle(curl_easy_init()) {
if(handle == nullptr) throw std::runtime_error("curl_easy_init failed");
// Set "this" as data pointer in callbacks to be able to make a call to the
// correct EasyCurly object. There are a lot more callback functions you
// could add here if you need them.
setopt(CURLOPT_WRITEDATA, this);
setopt(CURLOPT_DEBUGDATA, this);
setopt(CURLOPT_XFERINFODATA, this);
// Setup of proxy/callback functions. There should be one for each function
// above.
setopt(CURLOPT_WRITEFUNCTION, write_callback);
setopt(CURLOPT_DEBUGFUNCTION, debug_callback);
setopt(CURLOPT_XFERINFOFUNCTION, progress_callback);
// some default options, remove those you usually don't want
setopt(CURLOPT_NOPROGRESS, 0); // turn on progress callbacks
setopt(CURLOPT_FOLLOWLOCATION, 1L); // redirects
setopt(CURLOPT_HTTPPROXYTUNNEL, 1L); // corp. proxies etc.
// setopt(CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
setopt(CURLOPT_VERBOSE, 1L); // we want it all
}
// copy constructor
EasyCurly(const EasyCurly& other) : handle(curl_easy_duphandle(other.handle)) {
if(handle == nullptr) throw std::runtime_error("curl_easy_duphandle failed");
// State information is not shared when using curl_easy_duphandle. Only the
// options you've set (so you can create one CURL object, set its options and
// then use as a template for other objects. The document and debug data are
// therefor also not copied.
}
// move constructor
EasyCurly(EasyCurly&& other) :
handle(std::exchange(other.handle, nullptr)),
m_document(std::move(other.m_document)), m_debug(std::move(other.m_debug)) {}
// copy assignment
EasyCurly& operator=(const EasyCurly& other) {
CURL* tmp_handle = curl_easy_duphandle(other.handle);
if(handle == nullptr) throw std::runtime_error("curl_easy_duphandle failed");
// dup succeeded, now destroy any handle we might have and copy the tmp
curl_easy_cleanup(handle);
handle = tmp_handle;
return *this;
}
// move assignment
EasyCurly& operator=(EasyCurly&& other) {
std::swap(handle, other.handle);
std::swap(m_document, other.m_document);
std::swap(m_debug, other.m_debug);
return *this;
}
virtual ~EasyCurly() { curl_easy_cleanup(handle); }
// To be able to use an instance of EasyCurly with C interfaces if you don't add
// a function to this class for it, this operator will help
operator CURL*() { return handle; }
// generic curl_easy_setopt wrapper
template<typename T>
CURLcode setopt(CURLoption option, T v) {
return curl_easy_setopt(handle, option, v);
}
// perform by supplying url
CURLcode perform(std::string_view url) {
setopt(CURLOPT_URL, url.data());
return perform();
}
// perform with a previously supplied url
CURLcode perform() {
m_document.clear();
m_debug.clear();
return curl_easy_perform(handle);
}
// get collected data
std::string const& document() const { return m_document; }
std::string const& debug() const { return m_debug; }
// callbacks from proxy functions
virtual size_t on_write(char* ptr, size_t total_size) {
m_document.insert(m_document.end(), ptr, ptr + total_size);
// std::cout << "<write_callback> size " << total_size << "\n";
return total_size;
}
virtual int on_debug(curl_infotype /*type*/, char* data, size_t size) {
m_debug.insert(m_debug.end(), data, data + size);
// std::cout << "<debug>\n";
return 0; // must return 0
}
// override this to make a progress bar ...
virtual int on_progress(curl_off_t /*dltotal*/, curl_off_t /*dlnow*/,
curl_off_t /*ultotal*/, curl_off_t /*ulnow*/) {
// std::cout << "<progress>\n";
return 0;
}
private:
// a private class to initialize and cleanup once
class CurlGlobalInit {
public:
CurlGlobalInit() {
// std::cout << "CurlGlobalInit\n";
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if(res) throw std::runtime_error("curl_global_init failed");
}
~CurlGlobalInit() {
// std::cout << "~CurlGlobalInit\n";
curl_global_cleanup();
}
};
// callback functions - has to be static to work with the C interface in curl
// use the data pointer (this) that we set in the constructor and cast it back
// to a EasyCurly* and call the event handler in the correct object.
static size_t write_callback(char* ptr, size_t size, size_t nmemb,
void* userdata) {
EasyCurly* ecurly = static_cast<EasyCurly*>(userdata);
return ecurly->on_write(ptr, nmemb * size); // size==1 really
}
static int debug_callback(CURL* /*handle*/, curl_infotype type, char* data,
size_t size, void* userptr) {
EasyCurly* ecurly = static_cast<EasyCurly*>(userptr);
return ecurly->on_debug(type, data, size);
}
static int progress_callback(void* clientp, curl_off_t dltotal, curl_off_t dlnow,
curl_off_t ultotal, curl_off_t ulnow) {
EasyCurly* ecurly = static_cast<EasyCurly*>(clientp);
return ecurly->on_progress(dltotal, dlnow, ultotal, ulnow);
}
// resources
CURL* handle;
std::string m_document{};
std::string m_debug{};
// a static initializer object
static CurlGlobalInit setup_and_teardown;
};
// This must be defined in your .cpp file if you use this and split it into header
// and implementation parts.
EasyCurly::CurlGlobalInit EasyCurly::setup_and_teardown{};
// end of wrapper class
// --------------------------------------------------------------------------
// with that in place, the rest becomes something like this:
int main() {
EasyCurly curl; // our curl object
CURLcode res = curl.perform("http://www.google.com/");
std::cout << "result: " << res << "\n";
std::cout << "--- document size " << curl.document().size() << " captured ---\n"
<< curl.document() << "---------\n\n";
std::cout << "--- debug size " << curl.debug().size() << " captured ---\n"
<< curl.debug() << "---------\n";
}

thread-safety proxy with libcurl

#pragma once
#ifndef __CURL_CURL_H
#include "curl.h"
#endif
#ifndef __CURL_EASY_H
#include "easy.h"
#endif
#include <stdint.h>
#include <memory>
#include <string>
namespace CommUnit
{
enum ERR_PROXY
{
ERR_CURL_INIT_FAILED = 0xA0,
ERR_SET_PROXY_FAILED = 0xA1,
};
class MyProxy
{
public:
static MyProxy & GetInstance() //Meyers' Singlton
{
static MyProxy ProxySigleton;
return ProxySigleton;
}
public:
/*
* #bref:Get request
* #param[in] sUrl:Access URL
* #param[in] sProxyIp:Proxy IP
* #param[in] uProxyPort:Proxy Port
* #param[in] uTimeOut:Time out
* #param[in] isSSL:HTTPS true,else false
* #param[out] sRetContent:Return the URL content
*/
uint32_t Get(const std::string &sUrl,
const std::string& sProxyIp,
uint32_t uProxyPort,
uint32_t uTimeOut,
bool isSSL,
std::string &sRetContent);
private:
MyProxy(); //Constructor hidden
MyProxy(MyProxy const &); //Copy-Constructor hidden
MyProxy & operator= (MyProxy const &); //Assign operator hidden
~MyProxy(); //Destructor hidden
inline void _setCurlopt(CURL *pCurl,
const std::string &sUrl,
std::string &sWriterData,
const uint32_t uTimeOut,
bool isSSL);
//Callback function, write data to writerData
static int Writer(char *data,
uint32_t size,
uint32_t nmemb,
std::string *writerData);
private:
std::string m_sErrMsg;
static char s_ErrBuffer[CURL_ERROR_SIZE];
static const uint32_t m_MAXBUF = 2 * 1024 * 1024 - 128;
};
}
//////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include "MyProxy.h"
#include "Log.h"
#include <curl.h>
using namespace CommUnit;
char MyProxy::s_ErrBuffer[CURL_ERROR_SIZE] = { 0 };
MyProxy::MyProxy(void)
{
CURLcode oCURLcode = curl_global_init(CURL_GLOBAL_ALL);
if (oCURLcode != CURLE_OK)
{
Log("ERR: %s curl_init failed!", __func__);
}
}
MyProxy::~MyProxy(void)
{
curl_global_cleanup();
}
uint32_t MyProxy::Get(const std::string &sUrl,
const std::string& sProxyIp,
uint32_t uProxyPort,
uint32_t uTimeOut,
bool isSSL,
std::string &sRetContent)
{
sRetContent.clear();
CURL *pCurl = curl_easy_init();
CURLcode oCURLcode;
if (nullptr == pCurl)
{
Log("ERR: %s curl_easy_init failed!", __func__);
return ERR_CURL_INIT_FAILED;
}
_setCurlopt(pCurl, sUrl, sRetContent, uTimeOut, isSSL);
if (0 == sProxyIp.length()|| 0 == uProxyPort)
{
Log("ERR: %s SetProxy: ProxyIp [%s], ProxyPort[%u] failed",__func__, sProxyIp.c_str(), uProxyPort);
return ERR_SET_PROXY_FAILED;
}
Log("INFO: %s SetProxy: ProxyIp [%s], ProxyPort[%u] failed", __func__, sProxyIp.c_str(), uProxyPort);
curl_easy_setopt(pCurl, CURLOPT_PROXY, sProxyIp.c_str());
curl_easy_setopt(pCurl, CURLOPT_PROXYPORT, uProxyPort);
int iTimes = 0;
while (true)
{
oCURLcode = curl_easy_perform(pCurl);
if (oCURLcode != CURLE_OK && ++iTimes < 3)
usleep(5);
else
break;
}
if (oCURLcode != CURLE_OK)
{
Log("ERR: %s curl_easy_perform failed!", __func__);
}
curl_easy_cleanup(pCurl);
return oCURLcode;
}
void MyProxy::_setCurlopt(CURL *pCurl,
const std::string &sUrl,
std::string &sWriterData,
const uint32_t uTimeOut,
bool isSSL)
{
curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, s_ErrBuffer);
curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(pCurl, CURLOPT_URL, sUrl.c_str());
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, uTimeOut);
Log("INFO: %s Set Url:[%s],TimeOut:[%d]", __func__, sUrl.c_str(), uTimeOut);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, MyProxy::Writer);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &sWriterData);
//Skip peer and hostname verification
if (isSSL)
{
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0L);
}
}
int MyProxy::Writer(char *data,
uint32_t size,
uint32_t nmemb,
std::string *writerData)
{
if (writerData == nullptr)
{
Log("ERR: %s writerData is null!", __func__);
return 0;
}
int len = size * nmemb;
if ((writerData->size() + len) > m_MAXBUF)
{
Log("ERR: %s writerData size over MAXBUF!", __func__);
return 0;
}
writerData->append(data, len);
return len;
}
I want to realize a proxy with libcurl, which can get the content of given url(https). Morever, it need to be thread-safety.
But when I created 200 threads with pthreads to test my code, an segment fault occured sometimes.
How can I solve this problem?
Is there a relation with the sRetContent(std::string)?
Thanks!
Errmsg:
double free or corruption (!prev): 0x0ac72840 ***
Segmentation fault
My understanding is that libcurl is not thread-safe if you are using https (and it looks like you are) due to the fact that it is using underlying ssl libraries. See libcurl documentation and OpenSSL documentation for more info.
If your libcurl was compiled with OpenSSL for example then you have to initialize a few callback functions or you could run into issues. This is the sort of thing you need to do (compiles on Windows):
#include <curl/curl.h>
#include <openssl/crypto.h>
void win32_locking_callback(int mode, int type, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
{
WaitForSingleObject(lock_cs[type],INFINITE);
}
else
{
ReleaseMutex(lock_cs[type]);
}
}
void thread_setup(void)
{
int i;
lock_cs=(HANDLE*)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(HANDLE));
for (i=0; i<CRYPTO_num_locks(); i++)
{
lock_cs[i]=CreateMutex(NULL,FALSE,NULL);
}
CRYPTO_set_locking_callback((void (*)(int,int,const char *,int))win32_locking_callback);
}
void thread_cleanup(void)
{
int i;
CRYPTO_set_locking_callback(NULL);
for (i=0; i<CRYPTO_num_locks(); i++)
CloseHandle(lock_cs[i]);
OPENSSL_free(lock_cs);
}
I always call thread_setup() after my call to curl_global_init(CURL_GLOBAL_ALL)
and then thread_cleanup() right before my call to curl_global_cleanup().
I use this sort of code with libcurl often in load test scenarios and have never run into any issues. If you continue to run into problems, it is not libcurl, but something not being done properly in your code.
libcurl is thread-safe as long as you play by the rules

C++ query of JSON subObject error

my code compiles without error but at runtime I get the following error ->
testGetprice2.o: rapidjson/include/rapidjson/document.h:1125: rapidjson::SizeType rapidjson::GenericValue<Encoding, Allocator>::Size() const [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::SizeType = unsigned int]: Assertion `IsArray()' failed.
where it is failing is in my for () loop
std::cout << document[i]["lasttradeprice"].
I am trying to query for the subObject "lasttradeprice" but clearly accessing the object incorrectly.
How do I query the subobject correctly/get rid of this error?
code ->
#include "rapidjson/include/rapidjson/document.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <curl/curl.h>
#include <unistd.h>
#include <unordered_map>
#include <string>
using namespace rapidjson;
struct myData
{
std::fstream *file;
std::string *str;
};
size_t write_data(void *ptr, size_t size, size_t nmemb, myData *data)
{
size_t numBytes = size * nmemb;
if (data->file)
data->file->write((char*)ptr, numBytes);
if (data->str)
*data->str += std::string((char*)ptr, numBytes);
return numBytes;
}
//function to get coin data and perform analysis
int getData()
{
int count = 0;
//begin non terminating loop
while(true)
{
count++;
CURL *curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");
std::fstream file("/home/coinz/cryptsy/myfile.txt", std::ios_base::out | std::ios_base::ate);
std::string json;
myData data;
data.file = &file;
data.str = &json;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
{
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
else
{
file << std::endl;
//begin deserialization
Document document;
document.Parse(json.c_str());
for (SizeType i = 0; i < document.Size(); i++){
std::cout << document[i]["lasttradeprice"].GetString() << std::endl;
}
//assert(document.HasMember("return"));
//assert(document["return"].IsString());
//std::cout << "The Last Traded Price is = " << document["return"].GetString() << std::endl;
}
/* always cleanup */
curl_easy_cleanup(curl);
}
//timer for URL request. *ADUJST ME AS DESIRED*
usleep(10000000);
}
return 0;
}
//Le Main
int main(void)
{
getData();
}
gdb ->
(gdb) run
Starting program: /home/coinz/cryptsy/testGetprice2.o
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff5cc9700 (LWP 24708)]
[Thread 0x7ffff5cc9700 (LWP 24708) exited]
testGetprice2.o: rapidjson/include/rapidjson/document.h:1125: rapidjson::SizeType rapidjson::GenericValue<Encoding, Allocator>::Size() const [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>; rapidjson::SizeType = unsigned int]: Assertion `IsArray()' failed.
Program received signal SIGABRT, Aborted.
0x00007ffff6fec795 in raise () from /lib64/libc.so.6
replace your loop with
std::cout << "The Last Traded Price is = " << document["return"]["markets"]["DRK"]["lasttrad
eprice"].GetString() << std::endl;
According to the original JSON the structure is the following:
{
"success":1,
"return":
{
"markets":
{
"DRK":
{
"marketid":"155",
"label":"DRK\/BTC",
"lasttradeprice":"0.00553336",
"volume":"14615.30200941",
"lasttradetime":"2014-10-25 14:35:09",
"primaryname":"DarkCoin",
"primarycode":"DRK",
"secondaryname":"BitCoin",
"secondarycode":"BTC",
"recenttrades":
You are tying to access
document[i]["lasttradeprice"]
but you should access
documents["return"]["markets"][i]["lasttradedprice"]

json conversion error c++ IsString failed

I have data in a JSON object that I can't seem to get to.
Error:
> testGetprice3.o: testGetprice3.cpp:71: int getData(): Assertion
> `document["success"].IsString()' failed.
I have tried switching the datatypes, it only errors.
How do I correctly access my JSON object data?
#include "rapidjson/include/rapidjson/document.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <curl/curl.h>
#include <unistd.h>
#include <unordered_map>
#include <string>
using namespace rapidjson;
struct myData
{
std::fstream *file;
std::string *str;
};
size_t write_data(void *ptr, size_t size, size_t nmemb, myData *data)
{
size_t numBytes = size * nmemb;
if (data->file)
data->file->write((char*)ptr, numBytes);
if (data->str)
*data->str += std::string((char*)ptr, numBytes);
return numBytes;
}
//function to get coin data and perform analysis
int getData()
{
int count = 0;
//begin non terminating loop
while(true)
{
count++;
CURL *curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");
std::fstream file("/home/coinz/cryptsy/myfile.txt", std::ios_base::out | std::ios_base::ate);
std::string json;
myData data;
data.file = &file;
data.str = &json;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
/* Perform the request, res will get the return code */
CURLcode res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
{
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
else
{
file << std::endl;
//begin deserialization
Document document;
document.Parse(json.c_str());
assert(document.HasMember("success"));
assert(document["success"].IsString());
//std::cout << "The Last Traded Price is = " << document["lasttradeprice"].GetString() << std::endl;
}
/* always cleanup */
curl_easy_cleanup(curl);
}
//timer for URL request. *ADUJST ME AS DESIRED*
usleep(10000000);
}
return 0;
}
//Le Main
int main(void)
{
getData();
}
Looking at your JSON data, the success member is an int (as it is not surrounded by quote marks).
More info on JSON types can be found here: http://en.wikipedia.org/wiki/JSON#Data_types.2C_syntax_and_example
If the success member was a string, the JSON data would look like this: {"success":"1",...
Therefore when you call assert(document["success"].IsString()) it causes the error, as the success member is not a string, but an int.
Since the success member is an int, change the assertion to test whether it's an int - assert(document["success"].IsInt())
You stated you changed datatypes, but did not state which ones you tried. From the URL you are using, success is being returned as "success":1, which is an int.
Change:
assert(document["success"].IsString());
to:
assert(document["success"].IsInt());

CURL C API: callback was not called

The code below is a test for the CURL C API . The problem is that the callback function write_callback is never called. Why ?
/** compilation: g++ source.cpp -lcurl */
#include <assert.h>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <curl/curl.h>
using namespace std;
static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *userp)
{
std::cerr << "CALLBACK WAS CALLED" << endl;
exit(-1);
return size*nmemb;
}
static void test_curl()
{
int any_data=1;
CURLM* multi_handle=NULL;
CURL* handle_curl = ::curl_easy_init();
assert(handle_curl!=NULL);
::curl_easy_setopt(handle_curl, CURLOPT_URL, "http://en.wikipedia.org/wiki/Main_Page");
::curl_easy_setopt(handle_curl, CURLOPT_WRITEDATA, &any_data);
::curl_easy_setopt(handle_curl, CURLOPT_VERBOSE, 1);
::curl_easy_setopt(handle_curl, CURLOPT_WRITEFUNCTION, write_callback);
::curl_easy_setopt(handle_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
multi_handle = ::curl_multi_init();
assert(multi_handle!=NULL);
::curl_multi_add_handle(multi_handle, handle_curl);
int still_running=0;
/* lets start the fetch */
while(::curl_multi_perform(multi_handle, &still_running) ==
CURLM_CALL_MULTI_PERFORM );
std::cerr << "End of curl_multi_perform."<< endl;
//cleanup should go here
::exit(EXIT_SUCCESS);
}
int main(int argc,char** argv)
{
test_curl();
return 0;
}
Many thanks
Pierre
You need to check the value of still_running and call curl_multi_perform() again if there are still pending operations.
Simple example:
int still_running=0;
/* lets start the fetch */
do {
while(::curl_multi_perform(multi_handle, &still_running) ==
CURLM_CALL_MULTI_PERFORM);
} while (still_running);