鉴于此 cURL“POST”请求:
$ curl -i -s -k -X $'POST' \
-H $'Host: api.host.it' \
-H $'Content-Length: 205' \
-H $'Sec-Ch-Ua: \"Chromium\";v=\"93\", \" Not;A Brand\";v=\"99\"' \
-H $'Messageid: 9d6dd58d2df24d0aa410245a' \
-H $'Sessionid: ada9e560ed204e85a25e5475' \
-H $'Devicetype: ANDROID' \
-H $'Interactiondate-Date: 2021-09-27' \
-H $'Interactiondate-Time: 20:32:37.758' \
-H $'Sec-Ch-Ua-Mobile: ?0' \
-H $'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36' \
-H $'Content-Type: application/json;charset=UTF-8' \
-H $'Accept: application/json' \
-H $'Sourcesystem: WEB' \
-H $'Businessid: bbc0a98dc23a4a84968c42e4' \
-H $'Channel: HOSTWEBCO' \
-H $'Transactionid: 3F941666A8414D3C874AC77B' \
-H $'Sec-Ch-Ua-Platform: \"Linux\"' -H $'Origin: https://www.host.com' \
-H $'Sec-Fetch-Site: same-site' \
-H $'Sec-Fetch-Mode: cors' \
-H $'Sec-Fetch-Dest: empty' \
-H $'Referer: https://www.host.com/' \
-H $'Accept-Encoding: gzip, deflate' \
-H $'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' \
-H $'Connection: close' \
--data-binary $'{\"mount\":25,
\"Method\":\"SA\",
\"redirectUrlKo\":\"https://www.host.com/scarica?esito=KO\",
\"redirectUrlOk\":\"https://www.host.com/scarica?esito=OK\",
\"toMsisdn\":\"PARAMETER\",
\"txReqDescription\":\"scarica Online\"}' \
$'https://api.host.com/api/charge/public/init'
我需要创建一个 Bash 或 Python 脚本,为 file 内的每一行执行 cURL 命令numbers.txt
,将每一行作为 cURL 选项内显示的 PARAMETER 占位符的输入--data-binary
。在每个请求之后,仅当 HTTP 请求响应代码为“200 OK”时,才curl
应将输出定向到文件。output.txt
我知道 cURL 接受文件输入,[email protected]
但在此之前我还有其他字段,但它不起作用。
答案1
在下面的 shell 脚本中,我满足了您的 3 个要求:
PARAMETER
从文件中读取连续的值numbers.txt
,其中它们在一行中列出一个值。- 对于每个检索到的值,
PARAMETER
测试您的 HTTP 服务器请求是否返回代码“200” - 将 cURL 的连续输出附加到
output.txt
提供的 HTTP 返回代码为“200 OK”
就引号和转义引号而言,我没有修改您的语法。这可能是用例或特定于您的环境的,我无法对其进行测试。将以下 shell 脚本放入文件中,myscript.sh
并使用终端 cmd: 使该文件可执行chmod ug+x myscript.sh
:
$ cat myscript.sh
[输出]
#!/usr/bin/sh
output="$(/usr/bin/date +%Y%m%d_%H%M%S)""_output.txt"
while read -r dyn_param; do
set -- -isk \
-X 'POST' \
-H 'Host: api.host.it' \
-H 'Content-Length: 205' \
-H 'Sec-Ch-Ua: \"Chromium\";v=\"93\", \" Not;A Brand\";v=\"99\"' \
-H 'Messageid: 9d6dd58d2df24d0aa410245a' \
-H 'Sessionid: ada9e560ed204e85a25e5475' \
-H 'Devicetype: ANDROID' \
-H 'Interactiondate-Date: 2021-09-27' \
-H 'Interactiondate-Time: 20:32:37.758' \
-H 'Sec-Ch-Ua-Mobile: ?0' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36' \
-H 'Content-Type: application/json;charset=UTF-8' \
-H 'Accept: application/json' \
-H 'Sourcesystem: WEB' \
-H 'Businessid: bbc0a98dc23a4a84968c42e4' \
-H 'Channel: HOSTWEBCO' \
-H 'Transactionid: 3F941666A8414D3C874AC77B' \
-H 'Sec-Ch-Ua-Platform: \"Linux\"' \
-H 'Origin: https://www.host.com' \
-H 'Sec-Fetch-Site: same-site' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Referer: https://www.host.com/' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8' \
-H 'Connection: close'
set -- "$@" \
--data-binary '{"mount":25,"Method":"SA","redirectUrlKo":"https://www.host.com/ricarica?esito=KO","redirectUrlOk":"https://www.host.com/ricarica?esito=OK","toMsisdn":'\""$dyn_param"\"',"txReqDescription":"scarica Online"}'
httpcode=$(curl -o curl_tmp_dump -w "%{http_code}" "$@" https://api.host.com/api/recharge/public/init 2>/dev/null)
[ "$httpcode" -eq 200 ] && \cat curl_tmp_dump >> "$output"
\rm -f curl_tmp_dump
done < numbers.txt
exit 0