n 行の txt ファイルから n カール POST スクリプトを作成し、HTTP 要求応答「200 OK」の出力を条件付きで保存します。

n 行の txt ファイルから n カール POST スクリプトを作成し、HTTP 要求応答「200 OK」の出力を条件付きで保存します。

この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'

ファイルの各行に対してcURLコマンドを実行するBashスクリプトまたはPythonスクリプトを作成し、各行をnumbers.txtcURLオプション内に表示されているPARAMETERプレースホルダへの入力としてインポートする必要があります--data-binary。各要求の後に、curlHTTP要求応答コードが「200 OK」の場合にのみ出力をファイルに渡す必要があります。output.txt

cURLがファイル入力を許可していることはわかっていますが、[email protected]その前に別のフィールドがあるため機能しません。

ベストアンサー1

以下のシェルスクリプトは3つの要件を満たしています。

  • PARAMETERファイルから連続した値を読み込みますnumbers.txt。ここで、値は 1 行に 1 つの値としてリストされます。
  • 取得された各値について、PARAMETERHTTP サーバー要求がコード「200」を返すかどうかをテストします。
  • output.txt提供された HTTP 戻りコード "200 OK" に cURL の連続出力を追加します。

引用符を引用してエスケープする限り、構文は変更されませんでした。これはユースケースかもしれませんし、あなたの環境に固有のものかもしれません。テストできません。次のシェルスクリプトをファイルに入れ、端末cmd ::myscript.shを使用してファイルを実行可能にします。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

おすすめ記事