Created
May 2, 2019 12:44
-
-
Save sim51/a884bcdc2d749b25288ae403c270c29a to your computer and use it in GitHub Desktop.
Neo4j example in C code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <curl/curl.h> | |
#include <cjson/cJSON.h> | |
struct MemoryStruct { | |
char *memory; | |
size_t size; | |
}; | |
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) | |
{ | |
size_t realsize = size * nmemb; | |
struct MemoryStruct *mem = (struct MemoryStruct *)userp; | |
char *ptr = realloc(mem->memory, mem->size + realsize + 1); | |
if(ptr == NULL) { | |
/* out of memory! */ | |
printf("not enough memory (realloc returned NULL)\n"); | |
return 0; | |
} | |
mem->memory = ptr; | |
memcpy(&(mem->memory[mem->size]), contents, realsize); | |
mem->size += realsize; | |
mem->memory[mem->size] = 0; | |
return realsize; | |
} | |
int main(void) | |
{ | |
char *url = "http://localhost:7474/db/data/transaction/commit"; | |
char *neo4j_user = "neo4j"; | |
char *neo4j_admin = "admin"; | |
char *post_body = "{\"statements\":[{\"statement\":\"match (n) return toString(id(n)) AS txt LIMIT 10\"}]}"; | |
CURL *curl; | |
CURLcode res; | |
struct MemoryStruct chunk; | |
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ | |
chunk.size = 0; /* no data at this point */ | |
curl_global_init(CURL_GLOBAL_ALL); | |
curl = curl_easy_init(); | |
if (curl) | |
{ | |
/* First set the URL that is about to receive our POST. */ | |
curl_easy_setopt(curl, CURLOPT_URL, url); | |
/* Now specify we want to POST data */ | |
curl_easy_setopt(curl, CURLOPT_POST, 1L); | |
/* set user name and password for the authentication */ | |
curl_easy_setopt(curl, CURLOPT_USERPWD, "neo4j:admin"); | |
/* The request is a JSON document */ | |
struct curl_slist *headers = NULL; | |
headers = curl_slist_append(headers, "Accept: application/json"); | |
headers = curl_slist_append(headers, "Content-Type: application/json"); | |
headers = curl_slist_append(headers, "charsets: utf-8"); | |
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); | |
/* Send the Cypher query */ | |
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_body); | |
/* send all data to this function */ | |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); | |
/* we pass our 'chunk' struct to the callback function */ | |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); | |
/* get verbose debug output please */ | |
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); | |
/* Execute the CURL command */ | |
res = curl_easy_perform(curl); | |
/* Check for errors */ | |
if(res != CURLE_OK) | |
{ | |
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); | |
} | |
else | |
{ | |
printf("Receive => %s\n", chunk.memory); | |
/* Parsing the response as JSON */ | |
cJSON *json = cJSON_Parse(chunk.memory); | |
if (json == NULL) | |
{ | |
const char *error_ptr = cJSON_GetErrorPtr(); | |
if (error_ptr != NULL) | |
{ | |
fprintf(stderr, "Error before: %s\n", error_ptr); | |
} | |
} | |
else | |
{ | |
const cJSON *results = NULL; | |
const cJSON *result = NULL; | |
const cJSON *datas = NULL; | |
const cJSON *data = NULL; | |
const cJSON *row = NULL; | |
const cJSON *value = NULL; | |
results = cJSON_GetObjectItemCaseSensitive(json, "results"); | |
cJSON_ArrayForEach(result, results) | |
{ | |
/* For each line in the result */ | |
datas = cJSON_GetObjectItemCaseSensitive(result, "data"); | |
cJSON_ArrayForEach(data, datas) | |
{ | |
/* We take the row */ | |
row = cJSON_GetObjectItemCaseSensitive(data, "row"); | |
/* For each item in the row result */ | |
cJSON_ArrayForEach(value, row) | |
{ | |
printf("%s \t", value->valuestring); | |
} | |
printf("\n"); | |
} | |
} | |
} | |
cJSON_Delete(json); | |
} | |
/* Some cleanups */ | |
curl_easy_cleanup(curl); | |
free(chunk.memory); | |
curl_global_cleanup(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment