Javatpoint Logo
Javatpoint Logo

CouchDB HTTP API

HTTP requests are used to communicate with CouchDB.

By using these requests we can-

  • Retrieve data from the database
  • Store data in to the database in the form of documents
  • View as well as format the documents stored in a database.

HTTP Request Formats

There are different request formats used with Database while communicating. These request formats are: get, head, post, put, delete, and copy.

In CouchDB, the input data and output data structures will be in the form of JavaScript Object Notation (JSON) object for all operations.

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

GET : The GET request 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.

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

POST : The Post request is used to upload data. In CouchDB , the POST request is mainly used to set values, upload documents, set document values, and can also start certain administration commands.

PUT : The PUT request is used to create new objects, databases, documents, views and design documents.

DELETE : The DELETE request is used to delete documents, views, and design documents.

COPY : The COPY method is used to copy documents and objects.


HTTP Request Headers

HTTP headers should be supplied to get the right format and encoding. HTTP request headers are sent along with the request while sending the request to the CouchDB server. Following are the different Http request headers.

Content-type : The content-type 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.

Accept : The Accept 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.

In CouchDB, using Accept in queries is not required but it is highly recommended to ensure that the data returned can be processed by the client.


HTTP Response Headers

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

There are 4 types of response headers:

Content-type : The content-type response header specifies the MIME type of the data returned by the server. For most requests, the returned MIME type is text/plain.

Cache-control : The Cache-control response header is used to suggest 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.

Content-length : The content-length header is used to fetch the length of the content sent by the server, in bytes.

Etag : The Etag header is used to show the revision for a document, or a view.


Status Codes of HTTP Headers

The following table specifies the status codes sent by the http headers and their working:

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 paths to interact with database

Following is a list of some URL paths which are used to interact with the database directly.

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.




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA