Couchdb 简明教程

CouchDB - HTTP API

使用 HTTP 请求头,你可以与 CouchDB 通信。通过这些请求,我们可以从数据库中检索数据,以文档的形式将数据存储到数据库中,并且我们可以查看以及格式化存储在数据库中的文档。

Using HTTP request headers, you can communicate with CouchDB. Through these requests we can retrieve data from the database, store data in to the database in the form of documents, and we can view as well as format the documents stored in a database.

HTTP Request Formats

在与数据库通信时,我们将使用不同的请求格式,如 get、head、post、put、delete 和 copy。对于 CouchDB 中的所有操作,输入数据和输出数据结构将采用 JavaScript 对象表示法 (JSON) 对象的形式。

While communicating with the database we will use different request formats like get, head, post, put, delete, and copy. For all operations in CouchDB, the input data and the output data structures will be in the form of JavaScript Object Notation (JSON) object.

以下是用于与 CouchDB 通信的 HTTP 协议的不同请求格式。

Following are the different request formats of HTTP Protocol used to communicate with CouchDB.

  1. GET − This format is used to get a specific item. To get different items, you have to send specific url patterns. In CouchDB using this GET request, we can get static items, database documents and configuration, and statistical information in the form of JSON documents (in most cases).

  2. HEAD − The HEAD method is used to get the HTTP header of a GET request without the body of the response.

  3. POST − Post request is used to upload data. In CouchDB using POST request, you can set values, upload documents, set document values, and can also start certain administration commands.

  4. PUT − Using PUT request, you can create new objects, databases, documents, views and design documents.

  5. DELETE − Using DELETE request, you can delete documents, views, and design documents.

  6. COPY − Using COPY method, you can copy documents and objects.

HTTP Request Headers

应提供 HTTP 头部来获取正确的格式和编码。在向 CouchDB 服务器发送请求时,你可以随同请求发送 Http 请求头。以下是不同的 Http 请求标头。

HTTP headers should be supplied to get the right format and encoding. While sending the request to the CouchDB server, you can send Http request headers along with the request. Following are the different Http request headers.

  1. Content-type − This Header is used to specify the content type of the data that we supply to the server along with the request. Mostly the type of the content we send along with the request will be MIME type or JSON (application/json). Using Content-type on a request is highly recommended.

  2. Accept − This header is used to specify the server, the list of data types that client can understand, so that the server will send its response using those data types. Generally here, you can send the list of MIME data types the client accepts, separated by colons. Though, using Accept in queries of CouchDB is not required, it is highly recommended to ensure that the data returned can be processed by the client.

Response Headers

这些是由服务器发送的响应头。这些标头提供有关服务器作为响应发送的内容的信息。

These are the headers of the response sent by the server. These headers give information about the content send by the server as response.

  1. Content-type − This header specifies the MIME type of the data returned by the server. For most request, the returned MIME type is text/plain.

  2. Cache-control − This header suggests the client about treating the information sent by the server. CouchDB mostly returns the must-revalidate, which indicates that the information should be revalidated if possible.

  3. Content-length − This header returns the length of the content sent by the server, in bytes.

  4. Etag − This header is used to show the revision for a document, or a view.

Status Codes

以下是在 http 标头中发送的状态代码的表格形式及其说明。

Following is the tabular form of the status code sent by the http header and the description of it.

Sr.No.

Status Code & Description

1

200 − OK This status will be issued when a request completed successfully.

2

201 − Created This status will be issued when a document is created.

3

202 − Accepted This status will be issued when a request is accepted.

4

404 − Not Found This status will be issued when the server is unable to find the requested content.

5

405 − Resource Not Allowed This status is issued when the HTTP request type used is invalid.

6

409 − Conflict This status is issued whenever there is any update conflict.

7

415 − Bad Content Type This status indicated that the requested content type is not supported by the server.

8

500 − Internal Server Error This status is issued whenever the data sent in the request is invalid.

HTTP URL Paths

可以通过某些 URL 路径直接与数据库交互。以下是此类 URL 路径的表格形式。

There are certain url paths using which, you can interact with the database directly. Following is the tabular format of such url paths.

Sr.No.

URL & Operation

1

PUT /db This url is used to create a new database.

2

GET /db This url is used to get the information about the existing database.

3

PUT /db/document This url is used to create a document/update an existing document.

4

GET /db/document This url is used to get the document.

5

DELETE /db/document This url is used to delete the specified document from the specified database.

6

GET /db/_design/design-doc This url is used to get the definition of a design document.

7

GET /db/_design/designdoc/_view/view-name This url is used to access the view, view-name from the design document from the specified database.