curl上传文件

来源:互联网 发布:软件测试必备工具 编辑:程序博客网 时间:2024/06/05 15:14

在写上传文件的后台接口时,本来是想用postman测试的,不知道为什么,测试一直失败,上传的数据格式老是不对。后来想到,用curl来试试,好像还真成。

curl -F "key=value" -F "filename=@file.tar.gz" http://localhost/upload

如果使用了-F参数,curl会以multipart/form-data的方式发送POST请求。-F以key=value的形式指定要上传的参数,如果是文件,则需要使用key=@file的形式。

如果使用-d命令,curl会以application/x-www-url-encoded格式上传参数。

curl -d "action=del" -d "id=12" http://localhost/test

如果要以json格式上传参数,需要使用-H在命令中指定。

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost/api/login
0 0
原创粉丝点击