shell编程 - 发送请求
请求配置
host="http://localhost:3000"
baseUrl="/api/v1/test"
filePath="./data/data.json"
1. 发送get请求
- 默认发送get请求
url="${host}${baseUrl}"
curl $url
- 显式声明get请求
url="http://www.baidu.com"
token="2sjdkfdjiewe"
curl -X GET -H "token: ${token}" ${url}
2. 发送post请求
- 发送json数据
user="leon"
password="123456"
userdata="{\"Username\": \"${user}\", \"Password\": \"${password}\"}"
curl -X POST -d "${userdata}" -H "Content-Type: application/json" "${host}/api/login"
- 通过文件发送json数据
url="${host}${baseUrl}"
token="2sjdkfdjiewe"
curl -X POST -d "@${filePath}" -H "token: ${token}" -H "Content-Type: application/json" ${url}
- 发送formData 数据
url="${host}${baseUrl}"
curl -X POST -d 'login=leon' -d 'password=123' $url
# 将数据合并
curl -X POST -d 'login=leon&password=123' $url
3. 发送Delete请求
value1="1"
value2="2"
token="2sjdkfdjiewe"
url="${host}${baseUrl}?key1=${value1}&key2=${value2}"
curl -X DELETE -H "token: ${token}" ${url}
4. 发送PUT请求
value1="1"
value2="2"
token="2sjdkfdjiewe"
url="${host}${baseUrl}?key1=${value1}&key2=${value2}"
curl -X PUT -H "token: ${token}" ${url}
5. 上传文件
curl -F "filename=@./logo.png" -F "md5=9481fa162395122bc657a5577e509324" -H "multipart/form-data" http://localhost/api/Upload
6. 下载文件
curl -O $fileUrl