简体   繁体   中英

Uploading XML file to CouchDB through CURL via command-line

I'm failing to send a XML file named filesystem.xml to CouchDB using curl on the cmd-line.

I started by creating a new database named my_db :

curl -X PUT http://127.0.0.1:5984/my_db
{"ok":true}

and I've tried several ways to upload the xml document to this database so far, including:

curl -vX POST http://127.0.0.1:5984/my_db -H "Content-Type: text/xml" -d @filesystem.xml

which outputs:

* About to connect() to 127.0.0.1 port 5984 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 5984 (#0)
> POST /my_db HTTP/1.1
> User-Agent: curl/7.21.0 (i386-redhat-linux-gnu) libcurl/7.21.0 NSS/3.12.10.0 zlib/1.2.5 libidn/1.18 libssh2/1.2.4
> Host: 127.0.0.1:5984
> Accept: */*
> Content-Type: text/xml
> Content-Length: 3091
> Expect: 100-continue
> 
< HTTP/1.1 415 Unsupported Media Type
< Server: CouchDB/1.0.2 (Erlang OTP/R14B)
< Date: Thu, 01 Mar 2012 16:18:14 GMT
< Content-Type: text/plain;charset=utf-8
< Content-Length: 78
< Cache-Control: must-revalidate
< 
{"error":"bad_content_type","reason":"Content-Type must be application/json"}
* Connection #0 to host 127.0.0.1 left intact
* Closing connection #0

The response from the server says HTTP/1.1 415 Unsupported Media Type , which indicates the problem. How do I fix this?

I just recently started playing with CouchDB and curl so I know I must be missing something obvious. Feel free to elaborate your answer for this newbie.

What I found out is that you can't upload a file directly to the database. Files can only be stored if you ATTACH them to an existing document in the database.

The following instructions demonstrates what I was trying to do.

1) Create a dummy document:

curl -X POST http://localhost:5984/my_db -H "Content-Type: application/json" -d {}
{"ok":true,"id":"4f674a5b32bbd4e0a97f00817b0006d1","rev":"1-967a00dff5e02add41819138abb3284d"}

2) Upload XML file . You'll need the id and revision values that were returned by the previous command to do this:

curl -X PUT http://127.0.0.1:5984/my_db/4f674a5b32bbd4e0a97f00817b0006d1/attachment?rev=1-967a00dff5e02add41819138abb3284 --data-binary @filesystem.xml -H "Content-Type: text/xml"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM