libcurlからCypherクエリを発行する

ちょっとHTTPHEADERの設定のところで嵌っていた(見つけたサンプルが間違っていた・・・結局、curl.hを見て解決したけど)が、なんとかlibcurlを使ってREST APIからCypherクエリを発行し、Neo4jサーバから結果(JSON形式)を取得することができた。

備忘のためにそのときのコードの一部を書いておく。

  char* jsonObj = "{\"query\":\"START n=node(*) RETURN n.name,n.area? \" }";
  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Content-Type: application/json;");
  headers = curl_slist_append(headers, "Accept: application/json;");
  headers = curl_slist_append(headers, "charsets: utf-8;");
  headers = curl_slist_append(headers, "User-Agent: curl/7.29.0;");

  curl_easy_setopt( curl, CURLOPT_HTTPHEADER, headers );
  curl_easy_setopt( curl, CURLOPT_URL, "http://localhost:7474/db/data/cypher" );
  curl_easy_setopt(curl, CURLOPT_POST, 1L);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(jsonObj));
  curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)&wr_error );
  curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, write_data );

  /* Allow curl to perform the action */
  ret = curl_easy_perform( curl );
  
  printf( "ret = %d (write_error = %d)\n", ret, wr_error );

最後のprintf()で出力される結果はこんな感じになる。

ret = 0 (write_error = 0)
wr_buf:
{
  "columns" : [ "n.name", "n.area?" ],
  "data" : [ [ "ぬこ@横浜", "横浜市" ], ・・・(省略)・・・ ] ]
}

あとは返却されたJSONをパースしてResultSetとして使えるようにすれば、Neo4j-FDWのコアとなる部分は出来るはず。なんとか21日の勉強会までに実装が間に合わせたいな。