📜  CouchDB Http Api

📅  最后修改于: 2020-11-22 09:42:51             🧑  作者: Mango

CouchDB HTTP API

HTTP请求用于与CouchDB通信。

通过使用这些请求,我们可以-

  • 从数据库检索数据
  • 以文档形式将数据存储到数据库中
  • 查看和格式化存储在数据库中的文档。

HTTP请求格式

通信时,数据库使用不同的请求格式。这些请求格式为:获取,标题,发布,放置,删除和复制。

在CouchDB中,对于所有操作,输入数据和输出数据结构将采用JavaScript Object Notation(JSON)对象的形式。

查看用于与CouchDB通信的HTTP协议的不同请求格式。

GET:GET请求格式用于获取特定项目。要获得不同的商品,您必须发送特定的网址格式。在使用此GET请求的CouchDB中,我们可以以JSON文档的形式获取静态项,数据库文档和配置以及统计信息。

HEAD:HEAD方法用于获取GET请求的HTTP标头,而没有响应主体。

POST:POST请求用于上传数据。在CouchDB中,POST请求主要用于设置值,上传文档,设置文档值,还可以启动某些管理命令。

PUT:PUT请求用于创建新对象,数据库,文档,视图和设计文档。

DELETE:DELETE请求用于删除文档,视图和设计文档。

HTTP请求头

应该提供HTTP标头以获得正确的格式和编码。 HTTP请求标头与请求一起发送,同时将请求发送到CouchDB服务器。以下是不同的Http请求标头。

Content-type:Content-type Header用于指定我们与请求一起提供给服务器的数据的内容类型。通常,与请求一起发送的内容类型将是MIME类型或JSON(应用程序/ JSON)。强烈建议对请求使用Content-type。

Accept:Accept标头用于指定服务器,客户端可以理解的数据类型列表,以便服务器将使用这些数据类型发送其响应。通常,在这里,您可以发送客户端接受的MIME数据类型列表,以冒号分隔。

在CouchDB中,不需要在查询中使用“接受”,但是强烈建议确保客户端可以处理返回的数据。

HTTP响应头

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

响应头有4种类型:

Content-type:content-type响应标头指定服务器返回的数据的MIME类型。对于大多数请求,返回的MIME类型为text / plain。

高速缓存控制:高速缓存控制响应标头用于建议客户端处理服务器发送的信息。 CouchDB通常返回必须重新验证的信息,这表明信息应尽可能进行重新验证。

Content-length:content-length标头用于获取服务器发送的内容的长度(以字节为单位)。

Etag:Etag标头用于显示文档或视图的修订。

HTTP标头的状态码

下表指定了HTTP标头发送的状态代码及其工作方式:

Status Codes Description
200 – OK This status is generated when a request completed successfully.
201 – Created This status is generated when a document is created.
202 – Accepted This status is generated when a request is accepted.
404 – Not Found This status is issued when the server is unable to find the requested content.
405 – Resource Not Allowed This status is issued when the http request type used is invalid.
409 – Conflict This status is issued whenever there is any update conflict.
415 – Bad Content Type This status specifies that the requested content type is not supported by the server.
500 – Internal Server Error This status is issued whenever the invalid data is sent as request.

与数据库交互的HTTP URL路径

以下是一些URL路径的列表,这些URL路径用于直接与数据库进行交互。

URL Operation
PUT /db PUT url is used to create a new database.
GET /db GET url is used to get the information about the existing database.
PUT/db/document The PUT url is used to create a document/update an existing document.
GET /db/document The GET url is used to get the document.
DELETE /db/document The DELETE url is used to delete the specified document from the specified database.
GET/db/_design/design-doc This url is used to get the definition of a design document.
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.