You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by Apache Wiki <wi...@apache.org> on 2009/05/21 08:31:31 UTC
[Couchdb Wiki] Update of "Getting started with C" by Darrell Huang
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.
The following page has been changed by Darrell Huang:
http://wiki.apache.org/couchdb/Getting_started_with_C
------------------------------------------------------------------------------
## page was renamed from GettingStartedWithC
+ If you want to operate CouchDB with C, you must use HTTP requests. You can directly invoke socket to do that, or accomplish it with a library that encapsulates HTTP. CURL is one such library. To obtain a copy of the CURL library or information on it like its usage, just access its website http://curl.haxx.se/.
+
+ After CURL is installed, simply include its C header file as below
+ {{{
+ #include <curl/curl.h>
+ }}}
+ and link its library file (the file name of which contains string "libcurl") into the executable file.
+
+ The following code segment shows an example of operating CouchDB with CURL.
+ {{{
+ CURL *curl;
+ FILE *fp;
+ struct stat file_info;
+
+ /* the name of the file containing json data */
+ const char *const file_name = "data_source";
+
+ curl_global_init(CURL_GLOBAL_ALL);
+
+ stat(file_name, &file_info);
+ fp = fopen(file_name, "r");
+
+ curl = curl_easy_init();
+ if(curl) {
+ /* uses HTTP PUT */
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+
+ /* URL */
+ curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.0.40:5984/test_db/test_doc");
+
+ /* input file */
+ curl_easy_setopt(curl, CURLOPT_READDATA, fp);
+
+ /* file size */
+ curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)file_info.st_size);
+
+ /* data type */
+ struct curl_slist *slist = NULL;
+ slist = curl_slist_append(slist, "Content-Type: application/json");
+ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
+
+ /* makes the request */
+ curl_easy_perform(curl);
+
+ curl_slist_free_all(slist);
+
+ curl_easy_cleanup(curl);
+ }
+
+ fclose(fp);
+
+ curl_global_cleanup();
+ }}}
This one is horribly out of date. Don't read it. If you need a C API, you've got to write one. Then update this page accordingly, thanks :)