cURL 批量请求

req_params 文件中每一行都是一个 JSON 字符串,表示每次请求的请求体

1
2
3
4
5
6
{"a":"b", "c":"{\"d\":\"f\"}"}
{"a":"b", "c":"{\"d\":\"f\"}"}
{"a":"b", "c":"{\"d\":\"f\"}"}
{"a":"b", "c":"{\"d\":\"f\"}"}
{"a":"b", "c":"{\"d\":\"f\"}"}
{"a":"b", "c":"{\"d\":\"f\"}"}

现在需要针对每个请求体进行调用

1
2
3
4
5
# 重点在于 read 的参数 -r, 表示 raw 模式,不把输入的反斜杠字符解释为转义字符
# 如果不指定该参数,每行中的 \ 会被认为是转义字符,表示对后一个字符的转义,就不是一个合法的 JSON
while read -r line; do
curl -H 'Content-Type: application/json' -d "$line" http://192.168.31.224:8080/curl/print
done < req_params

使用 here document 进行请求参数的填充

1
2
3
4
5
6
7
8
while read ph; do
input=$(cat << EOF
{"phone":"${ph}"}
EOF
)
curl -H 'Content-Type: application/json' -d "${input}" http://192.168.31.224:8080/curl/print
echo
done < phone

read 命令 - Bash 脚本教程 - 网道 (wangdoc.com)