2013-03-12 30 views
0

我正在測試我的RESTFul API,我想知道是否有辦法同時上傳文件和json數據。Curl命令頂部上傳文件與元數據

當我運行命令行命令:

curl -v -X POST -d '{"label":"vacations", "date":"2011-01-03"}' -F [email protected]"photo.png" http://localhost/album

我得到這個錯誤: Warning: You can only select one HTTP request!

是否真的做到這一點的方法嗎?

+0

可我也用@sputnick方式上傳的音頻文件的元數據? – wguerrero 2013-03-12 18:31:14

+0

當然wguerrero,你可以用這種方法上傳你想要的任何二進制文件。 – 2013-03-12 22:13:03

回答

0

看來你有

-F [email protected]"photo.png" 

-d '{"label":"vacations", "date":"2011-01-03"}' 

之間的衝突,你可以使用這個2個選項只有一個。

我認爲最好的辦法是base64 encode在字符串中的圖片,並把它放在你的JSON,如:

-d '{"label":"vacations", "date":"2011-01-03", "photo":"AE..."}' 

base64 decode它的服務器端。

您也可以添加此開關:

-H 'Content-type:text/json' 
+0

謝謝。你知道我可以編碼文件的在線服務嗎?我的意思是編碼的文件是有效的。 – wguerrero 2013-03-12 17:44:12

+0

你使用哪個操作系統?在Linux上,我可以使用'base64文件' – 2013-03-12 17:46:28

+0

是的,我使用Linux – wguerrero 2013-03-12 17:47:24

0

@sputnick的aproach工程:)。

我會用postman(一個擴展名爲chrome)來測試我的restful API而不是curl。

0

您還可以使用-F的元數據,請參見埃裏克Allik的答案here

類似例如:

curl -F 'json={"label":"vacations", "date":"2011-01-03"}' -F '[email protected]' http://localhost/album