Couchdb 简明教程

CouchDB - Curl & Futon

cURL Utility

cURL 实用程序是一种与 CouchDB 通信的方法。

它是一种使用支持协议(HTTP、HTTPS、FTP、FTPS、TFTP、DICT、TELNET、LDAP 或 FILE)之一,从服务器或向服务器传输数据的工具。这个命令设计为无需用户互动即可工作。cURL 提供了一系列的实用技巧,如代理支持、用户认证、ftp 上传、HTTP Post、SSL (https:) 连接、cookie、文件传输恢复等。

cURL 实用程序在 UNIX、Linux、Mac OS X 和 Windows 等操作系统中可用。它是一个命令行实用程序,用户可以用它直接从命令行访问 HTTP 协议。本章将指导你如何使用 cURL 实用程序。

Using cURL Utility

你可以通过简单地输入 cURL 及其后跟网站地址来使用 cURL 实用程序访问任何网站,如下所示:−

curl www.tutorialspoint.com/

默认情况下,cURL 实用程序返回请求页面源代码。它在终端窗口中显示此代码。

cURL Utility Options

cURL 实用程序提供多种选项供使用,你可以在 cURL 使用帮助中查看它们。

以下代码展示了 cURL 帮助的其中一部分。

$ curl --help
Usage: curl [options...] <url>
Options: (H) means HTTP/HTTPS only, (F) means FTP only
      --anyauth Pick "any" authentication method (H)
   -a/--append Append to target file when uploading (F/SFTP)
      --basic Use HTTP Basic Authentication (H)
      --cacert <file> CA certificate to verify peer against (SSL)
-d/--data <data> HTTP POST data (H)
      --data-ascii <data> HTTP POST ASCII data (H)
      --data-binary <data> HTTP POST binary data (H)
      --data-urlencode <name=data/name@filename> HTTP POST data
urlencoded (H)
      --delegation STRING GSS-API delegation permission
      --digest Use HTTP Digest Authentication (H)
      --disable-eprt Inhibit using EPRT or LPRT (F)
      --disable-epsv Inhibit using EPSV (F)

   -F/--form <name=content> Specify HTTP multipart POST data (H)
      --form-string <name=string> Specify HTTP multipart POST data (H)
      --ftp-account <data> Account data to send when requested by server
(F)
      --ftp-alternative-to-user <cmd> String to replace "USER [name]" (F)
      --ftp-create-dirs Create the remote dirs if not present (F)
      --ftp-method [multi cwd/no cwd/single cwd] Control CWD usage (F)
      --ftp-pasv Use PASV/EPSV instead of PORT (F)

   -G/--get Send the -d data with a HTTP GET (H)

   -H/--header <line> Custom header to pass to server (H)
   -I/--head Show document info only
   -h/--help This help text
      --hostpubmd5 <md5> Hex encoded MD5 string of the host public key.
(SSH)
   -0/--http1.0 Use HTTP 1.0 (H)
      --ignore-content-length Ignore the HTTP Content-Length header
   -i/--include Include protocol headers in the output (H/F)

   -M/--manual Display the full manual

   -o/--output <file> Write output to <file> instead of stdout
      --pass <pass> Pass phrase for the private key (SSL/SSH)
      --post301 Do not switch to GET after following a 301
redirect (H)
      --post302 Do not switch to GET after following a 302
redirect (H)
   -O/--remote-name Write output to a file named as the remote file
      --remote-name-all Use the remote file name for all URLs
   -R/--remote-time Set the remote file's time on the local output
   -X/--request <command> Specify request command to use
      --retry <num> Retry request <num> times if transient problems
occur
      --retry-delay <seconds> When retrying, wait this many seconds
between each
      --retry-max-time <seconds> Retry only within this period
   -T/--upload-file <file> Transfer <file> to remote site
      --url <URL> Set URL to work with
   -B/--use-ascii Use ASCII/text transfer

在与 CouchDB 通信时,我们大量地使用了 cURL 实用程序的某些选项。以下是 cURL 实用程序部分重要选项的简要说明,包括 CouchDB 使用的选项。

-X flag

(HTTP) 指定在与 HTTP 服务器通信时使用的自定义请求方法。已指定请求代替已使用的方法(默认为 GET)。完整信息和解释,请阅读 HTTP 1.1 规范。

(FTP) 与使用 ftp 进行文件列表时指定一个自定义 FTP 命令,以代替 LIST。

-H

(HTTP) 获取网页时使用额外的报头。注意,如果你添加了一个自定义报头,该报头与 cURL 将要使用的内部报头具有相同的名称,那么你的外部设置报头将代替内部报头。这使你可以完成 cURL 通常无法完成的即使更加棘手的操作。你不应该在不明白自己所做的事情的情况下替换内部设置报头。用冒号右侧没有内容的报头替换内部报头将防止该报头出现。

cURL 确保你添加/替换的每个报头使用适当的行尾标记发送。你也不应该将此作为报头内容的一部分添加,也不应该添加换行符或回车符来弄乱。

另请参阅 -A/--user-agent 和 -e/--referer 选项。

此选项可以多次使用,以添加/替换/删除多个报头。

-d flag

使用 cURL 的此标记,你可以将数据与 HTTP POST 请求一起发送到服务器,就好像用户通过表单填写并提交数据一样。

Example

假设有一个网站,你想使用以下 cURL 实用程序的 –d 标记登录其中或向该网站发送一些数据。

curl -X PUT http://mywebsite.com/login.html -d userid=001 -d password=tutorialspoint

它发送一个看起来像 "userid=001&password=tutorialspoint" 的 post 块。同样地,你也可以使用 -d 标记发送文档 (JSON)。

-o flag

使用此标志,cURL 将请求输出写入文件。

Example

以下示例显示了 cURL 实用工具的 -o 标志的用法。

$ curl -o example.html www.tutorialspoint.com/index.htm
% Total % Received % Xferd Average Speed Time Time Time Current
      Dload Upload Total Spent Left Speed
100 81193 0 81193 0 0 48168 0 --:--:-- 0:00:01 --:--:--
58077

它获取 tutorialspoint.com 主页的源代码,创建一个名为 example.com 的文件并将输出保存在名为 example.html 的文件中。

以下是 example.html 的快照。

example html

-O

此标志类似于 –o ,唯一的区别是使用此标志,会创建一个与请求的 URL 同名的文件,并将请求的 URL 的源代码复制到其中。

Example

以下示例显示了 cURL 实用工具的 -O 标志的用法。

$ curl -O www.tutorialspoint.com/index.htm
% Total % Received % Xferd Average Speed Time Time Time Current
      Dload Upload Total Spent Left
Speed
100 81285 0 81285 0 0 49794 0 --:--:-- 0:00:01 --:--:--
60077

它创建一个名为 index.htm 的文件,并将 tutorialspoint.com 索引页的源代码保存在其中。

Hello CouchDB

您可以通过向已安装的 CouchDB 实例发送 GET 请求来访问 CouchDB 主页。首先,确保您已在 Linux 环境中安装 CouchDB 并且它运行成功,然后使用以下语法向 CouchDB 实例发送 get 请求。

curl http://127.0.0.1:5984/

这会向您提供如以下所示的 JSON 文档,其中 CouchDB 指定了详细信息,例如版本号、供应商名称和软件版本。

$ curl http://127.0.0.1:5984/
{
   "couchdb" : "Welcome",
   "uuid" : "8f0d59acd0e179f5e9f0075fa1f5e804",
   "version" : "1.6.1",
   "vendor" : {
      "name":"The Apache Software Foundation",
      "version":"1.6.1"
   }
}

List of All Databases

您可以发送 get 请求以及字符串 "_all_dbs string " ,以获取所创建的所有数据库的列表。以下是要获取 CouchDB 中所有数据库列表的语法。

curl -X GET http://127.0.0.1:5984/_all_dbs

它向您提供以下所示的 CouchDB 中所有数据库的列表。

$ curl -X GET http://127.0.0.1:5984/_all_dbs
[ "_replicator" , "_users" ]

Creating a Database

您可以使用具有以下语法的 PUT 标头的 cURL 在 CouchDB 中创建数据库 −

$ curl -X PUT http://127.0.0.1:5984/database_name

Example

例如,使用上述给定的语法,创建一个名为 my_database 的数据库,如下所示。

$ curl -X PUT http://127.0.0.1:5984/my_database
{"ok":true}

Verification

通过列出所有数据库,验证数据库是否已创建,如下所示。在这里,您可以在列表中看到新创建的数据库 "my_database" 的名称。

$ curl -X GET http://127.0.0.1:5984/_all_dbs

[ "_replicator " , "_users" , "my_database" ]

Getting Database Info

您可以使用 get 请求以及数据库名称来获取数据库的信息。以下是要获取数据库信息的语法。

Example

例如让我们获取名为 my_database 的数据库的信息,如下所示。在这里,您可以得到您的数据库信息的响应。

$ curl -X GET http://127.0.0.1:5984/my_database

{
   "db_name" : "my_database",
   "doc_count" : 0,
   "doc_del_count" : 0,
   "update_seq" : 0,
   "purge_seq" : 0,
   "compact_running" : false,
   "disk_size" : 79,
   "data_size" : 0,
   "instance_start_time" : "1423628520835029",
   "disk_format_version" : 6,
   "committed_update_seq" : 0
 }

Futon

Futon 是 CouchDB 内置的基于 Web 的管理界面。它提供了一个简单的图形界面,可用于与 CouchDB 交互。它是一个朴素的界面,并提供了对所有 CouchDB 功能的完全访问。以下是这些功能的列表 −

  1. Creates databases.

  2. Destroys databases.

  3. Creates documents.

  4. Updates documents.

  5. Edits documents.

  6. Deletes documents.

Starting Futon

确保 CouchDB 正在运行,然后在浏览器中打开以下 URL −

http://127.0.0.1:5984/_utils/

如果您打开此 URL,它将显示以下所示的 Futon 主页 −

futon homepage
  1. On the left hand side of this page you can observe the list of all the current databases of CouchDB. In this illustration, we have a database named my_database, along with system defined databases _replicator and _user.

  2. On the right hand side you can see the following − Tools − In this section you can find Configuration to configure CouchDB, Replicator to perform replications, and Status to verify status of CouchDB and recent modifications done on CouchDB. Documentation − This section contains the complete documentation for the recent version of CouchDB. Diagnostics − Under this you can verify the installation of CouchDB. Recent Databases − Under this you can find the names of recently added databases.