To communicate with CouchDB in order to retrieve data from the database, to store data into the database, to view the stored documents and to format the documents stored in a database, the HTTP requests are used. The input data and output data structures, for any operation in CouchDB, are in the form of JavaScript Object Notation (JSON) object.
HTTP Request Formats:
To communicate with CouchDB different request formats of HTTP Protocol are used, including:
REQUEST FORMAT | USES |
GET | ● To get static items.
● To get statistical information in the form of JSON documents. ● To get database documents and configuration. |
HEAD | ● To get the HTTP header of a GET request without the body of the response. |
POST | ● To set values.
● To upload documents. ● To set document values. ● To start certain administration commands. |
PUT | ● To create new Objects, Databases, Documents, Views and Design documents. |
DELETE | ● To delete Documents, Views and Design documents. |
COPY | ● To copy documents and objects. |
HTTP Request Headers:
To get the right format and encoding, sending HTTP request headers along with the request is a must while sending the request to the CouchDB server. There are mainly two types of HTTP request headers. These are:
Content-type:
- Used to specify the content type of the data to be sent to the server along with the request.
- The type of content sent is mostly MIME type or JSON (application/JSON).
- Highly recommended.
Accept:
- Used to specify the list of data types that client can understand to the server.
- The server thus sends its response using the specified data types. A list of MIME data types, separated by colons can be sent.
- Using Accept in queries is not required in CouchDB.
- But to ensure that the data returned can be processed by the client, this request header is highly recommended.
HTTP Response Headers:
The HTTP response headers are used to get information about the content sent by the server as a response as they itself are a type of response sent by the server. There are mainly four types of HTTP Response headers. These are:
Content-type:
- Used to define the MIME type of the data sent by the server which is text/plain for most requests.
Cache-control:
- Used to suggest the client about treating the information sent by the server.
- If must-revalidate is returned, then the information should be revalidated if possible.
Content-length:
- Used to retrieve the length of the content sent by the server.
- It retrieves the length in bytes.
Etag:
- Used to display the revision for a document or a view.
Status Codes of HTTP Headers:
STATUS CODE | DESCRIPTION |
200 – OK | Generated when a request completed successfully. |
201 – Created | Generated when a document is created. |
202 – Accepted | Generated when a request is accepted. |
404 – Not Found | Issued when the server is unable to find the requested content. |
405 – Resource Not Allowed | Issued when the http request type used is invalid. |
409 – Conflict | Issued whenever there is any update conflict. |
415 – Bad Content Type | To specify that the requested content type is not supported by the server. |
500 – Internal Server Error | Issued whenever the invalid data is sent as request. |
HTTP URL paths to interact with database:
URL | USES |
PUT /db | To create a new database. |
GET /db | To get the information about the existing database. |
PUT/db/document | To create a document/update an existing document. |
GET /db/document | To get a document from the specified database. |
DELETE /db/document | To delete a document from the specified database. |
GET/db/_design/design-doc | To get the definition of a design document. |
GET /db/_design/designdoc/_view/view-name | To access the view, view-name of a design document. |