September 1, 2019
By: Dirk
使用 Curl 上传文件
CURL 用来测试 Rest API 非常方便,在此说下如何使用 curl 进行文件上传操作。
使用 Curl 进行文件上传时使用 -F(--from) ,加上此参数后相当于在请求中添加了 enctype="multipart/form-data" 参数。
单文件上传
上传 /home/dirk/hello.txt 文件到 http://localhost/file/upload 路径,参数名称为 file,并指定 file_prefix 值为 test
curl -X POST -F 'file=@/home/dirk/hello.txt' -F 'file_prefix=test' http://localhost/file/upload
多文件上传
多文件上传只需要再指定一个 -F 参数即可,比如上传文件参数为 file1 和 file2
curl -X POST -F 'file=@/home/dirk/hello1.txt' -F 'file=@/home/dirk/hello2.txt' http://localhost/file/upload
文件数组上传
多个文件是以数组的形式上传到一个参数中
curl -X POST -F 'files=@/home/dirk/hello1.txt' -F 'files=@/home/dirk/hello2.txt' http://localhost/file/upload