场景:
需要写一个脚本,将本地的文件通过http服务端的一个上传文件接口上传到服务端。
例:
有一个http服务端,有一个上传文件接口“/uploadUpgradeFile”,写一个脚本将本地的文件通过模拟前端的form表单将文件发送到服务端的上传文件接口。
知识点:
curl ,curl的post,curl的form表单域字段,shell脚本单引号如何使用变量。
例子:
命令行
./upload_file.sh /opt/test.zip
脚本文件 upload_file.sh
#/bin/bash
curl -X POST 'http://127.0.0.1:8888/uploadUpgradeFile' \
--header 'Cookie: userId=66666666666;' \
--header 'Content-Type: multipart/form-data' \
--form 'file=@'$1';filename=nameinpost' \
--form 'token=ww-aa-450c-bb-cc'
1.curl form表单中文件如何表示
This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign.
简单意思就是前缀使用@符号来表示文件。curl文档地址:
https://tool.oschina.net/uploads/apidocs/curl7.26/curl.html#-F
2.表单域中其它字段如何表示
仍然使用--form 'key-value'的形式,但是在file=@fileLocation中,可以用";filename="来显式更改文件名。
3.单引号中使用变量
用两个单引号包裹变量,其实应该是将变量前后分隔成两个部分。$1接收命令行中第一个参数,例子中是文件的地址。