2012-03-01 153 views
1

我無法使用cmd-line上的curl將名爲filesystem.xml的XML文件發送到CouchDB。通過命令行通過CURL將XML文件上傳到CouchDB

我開始通過創建MY_DB命名一個新的數據庫:

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

我嘗試了幾種方法來上傳XML文檔到該數據庫,到目前爲止,包括:

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

其輸出:

* 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 

響應fr om服務器說HTTP/1.1 415 Unsupported Media Type,這表明問題。我該如何解決?

我剛剛開始玩CouchDB和捲曲,所以我知道我必須錯過明顯的東西。隨時爲這個新手精心製作你的答​​案。

+0

您發佈到數據庫中,你想要做的是把一個附件上的文檔。 – 2012-03-01 17:18:33

+0

謝謝,我結束了幾分鐘前的發現。我會在未來發佈一個幫助他人的答案。 – karlphillip 2012-03-01 17:30:27

回答

0

我發現的是,你不能直接上傳文件到數據庫。只有將文件附加到數據庫中的現有文檔時,才能存儲文件。

以下說明演示了我正在嘗試做的事情。

1)創建一個虛擬文件:

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

2)上傳的XML文件。你需要的ID版本由先前的命令所返回的值要做到這一點:

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"