1.4. cURL: Your Command Line Friend

The curl utility is a command line tool available on Unix, Linux,Mac OS X and Windows and many other platforms. curl provides easyaccess to the HTTP protocol (among others) directly from thecommand-line and is therefore an ideal way of interacting with CouchDBover the HTTP REST API.

For simple GET requests you can supply the URL of the request. Forexample, to get the database information:

  1. shell> curl http://127.0.0.1:5984

This returns the database information (formatted in the output below forclarity):

  1. {
  2. "couchdb": "Welcome",
  3. "version": "2.0.0",
  4. "vendor": {
  5. "name": "The Apache Software Foundation"
  6. }
  7. }

Note

For some URLs, especially those that include special characters such asampersand, exclamation mark, or question mark, you should quote the URL youare specifying on the command line. For example:

  1. shell> curl 'http://couchdb:5984/_uuids?count=5'

Note

On Microsoft Windows, use double-quotes anywhere you see single-quotes inthe following examples. Use doubled double-quotes (“”) anywhere you seesingle quotes. For example, if you see:

  1. shell> curl -X PUT 'http:/127.0.0.1:5984/demo/doc' -d '{"motto": "I love gnomes"}'

you should replace it with:

  1. shell> curl -X PUT "http://127.0.0.1:5984/demo/doc" -d "{""motto"": ""I love gnomes""}"

If you prefer, ^" and \" may be used to escape the double-quotecharacter in quoted strings instead.

You can explicitly set the HTTP command using the -X command line option.For example, when creating a database, you set the name of the database in theURL you send using a PUT request:

  1. shell> curl -X PUT http://127.0.0.1:5984/demo
  2. {"ok":true}

But to obtain the database information you use a GET request (withthe return information formatted for clarity):

  1. shell> curl -X GET http://127.0.0.1:5984/demo
  2. {
  3. "compact_running" : false,
  4. "doc_count" : 0,
  5. "db_name" : "demo",
  6. "purge_seq" : 0,
  7. "committed_update_seq" : 0,
  8. "doc_del_count" : 0,
  9. "disk_format_version" : 5,
  10. "update_seq" : 0,
  11. "instance_start_time" : "0",
  12. "disk_size" : 79
  13. }

For certain operations, you must specify the content type of request, which youdo by specifying the Content-Type header using the -H command-lineoption:

  1. shell> curl -H 'Content-Type: application/json' http://127.0.0.1:5984/_uuids

You can also submit ‘payload’ data, that is, data in the body of the HTTPrequest using the -d option. This is useful if you need to submit JSONstructures, for example document data, as part of the request. For example, tosubmit a simple document to the demo database:

  1. shell> curl -H 'Content-Type: application/json' \
  2. -X POST http://127.0.0.1:5984/demo \
  3. -d '{"company": "Example, Inc."}'
  4. {"ok":true,"id":"8843faaf0b831d364278331bc3001bd8",
  5. "rev":"1-33b9fbce46930280dab37d672bbc8bb9"}

In the above example, the argument after the -d option is the JSON of thedocument we want to submit.

The document can be accessed by using the automatically generated document IDthat was returned:

  1. shell> curl -X GET http://127.0.0.1:5984/demo/8843faaf0b831d364278331bc3001bd8
  2. {"_id":"8843faaf0b831d364278331bc3001bd8",
  3. "_rev":"1-33b9fbce46930280dab37d672bbc8bb9",
  4. "company":"Example, Inc."}

The API samples in the API Basics show the HTTP command, URL and anypayload information that needs to be submitted (and the expected return value).All of these examples can be reproduced using curl with the command-lineexamples shown above.

原文: http://docs.couchdb.org/en/stable/intro/curl.html