How do I make RPC calls from C?
My question is related to the question: "How do I call JSON RPC API using C#?"
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It only takes a minute to sign up.
Sign up to join this communityHow do I make RPC calls from C?
My question is related to the question: "How do I call JSON RPC API using C#?"
The C API for processing JSON is Jansson. C applications like libblkmaker
use cURL for making the calls and Jansson for interpreting the JSON that cURL fetches.
For example basic usage (which can be easily modified for Bitcoin RPC), see the Jansson example github_commits.c
and the associated tutorial.
Here's an example of how to make JSON RPC calls with cURL, the output of which Jansson could process:
#include <stdlib.h>
#include <curl/curl.h>
int main()
{
CURL *curl = curl_easy_init();
struct curl_slist *headers = NULL;
if (curl) {
const char *data =
"{\"jsonrpc\": \"1.0\", \"id\":\"curltest\", \"method\": \"getinfo\", \"params\": [] }";
headers = curl_slist_append(headers, "content-type: text/plain;");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8332/");
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(data));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(curl, CURLOPT_USERPWD,
"bitcoinrpcUSERNAME:bitcoinrpcPASSWORD");
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
curl_easy_perform(curl);
}
return 0;
}
It will print, like the cURL example, something like this:
{"result":{"balance":0.000000000000000,"blocks":59952,"connections":48,"proxy":"","generate":false, "genproclimit":-1,"difficulty":16.61907875185736},"error":null,"id":"curltest"}