I am trying to send a request to my bitcoin full node. If I type this in my command line:
curl --user user:pw --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockcount", "params": []}' -H 'content-type: text/plain;' http://127.0.0.1:8332/
I get the answer I want:
{"result":709554,"error":null,"id":"curltest"}
However if I try to replicate the exact same command in C++ using libcurl I always get an empty answer:
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <stdlib.h>
struct MemoryStruct {
std::string cont = "";
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
mem->cont += (char *) contents;
return size * nmemb;
}
void getblockcount(std::string user, std::string pw){
CURL *curl;
CURLcode res;
struct curl_slist *list = NULL;
curl = curl_easy_init();
struct MemoryStruct chunk;
chunk.cont = "";
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8332");
list = curl_slist_append(list, "content-type: text/plain;");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_USERPWD, user + ":" + pw);
std::string json_req = "{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"getblockcount\", \"params\": []}";
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) json_req.length());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_req.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
res = curl_easy_perform(curl);
std::cout << "chunksize = " << chunk.cont.length() << std::endl;
std::cout << "answer =" << chunk.cont<< std::endl;
/* Check for errors */
if(res != CURLE_OK){
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}else{
std::cout << "curl was a success" << std::endl;
}
curl_slist_free_all(list);
curl_easy_cleanup(curl);
}
int main(int argc, char** argv){
curl_global_init(CURL_GLOBAL_ALL);
getblockcount("user", "pw");
curl_global_cleanup();
return 0;
}
results in
chunksize = 0
answer =
curl was a success
I am replicating the same call with the same underlying library. Why don't i get the result? What am I missing?
Aucun commentaire:
Enregistrer un commentaire