簡體   English   中英

Bash CURL GET Request output to a single JSON file including server response and CURL Information

[英]Bash CURL GET Request output to a single JSON file including server response and CURL Information

I am performing a curl test using shell script where I need to save curl server response into JSON along with CURL Information like time_connect, http_code, etc. I am trying the following code to write output to a JSON

  HOST_ADDR="http://$mynds/"

  do_request(){
       echo $(curl $HOST_ADDR --silent -w "\n{\n
        \"HttpCode\": %{http_code},\n
        \"NumRedirects\":%{num_redirects},\n
        \"NumConnects\":%{num_connects},\n
        \"SizeDownloadInBytes\":%{size_download},\n
        \"SizeHeaderInBytes\":%{size_header},\n
        \"SizeRequestInBytes\":%{size_request},\n
        \"SizeUploadInBytes\":%{size_upload},\n
        \"SpeedUploadBPS\":%{speed_upload},\n
        \"SpeedDownloadBPS\":%{speed_download},\n
        \"TimeAppConnectSec\":%{time_appconnect},\n
        \"TimeConnectSec\":%{time_connect},\n
        \"TimeNamelookupSec\":%{time_namelookup},\n
        \"TimePreTransferSec\":%{time_pretransfer},\n
        \"TimeRedirectSec\":%{time_redirect},\n
        \"TimeStartTransferSec\":%{time_starttransfer},\n
        \"TimeTotalSec\":%{time_total},\n
        \"UrlEffective\":\"%{url_effective}\"
 }" -s)
}

do_request

我得到的簡單 output :

{"hostIPAddr":"0.0.0.0","hostname":"vm01","text":"Hello World from 
vm01"}` and `{ "HttpCode": 200, "NumRedirects":0, "NumConnects":1, 
"SizeDownloadInBytes":85, "SizeHeaderInBytes":263, 
"SizeRequestInBytes":99, "SizeUploadInBytes":0, 
"SpeedUploadBPS":0.000, "SpeedDownloadBPS":14.000, 
"TimeAppConnectSec":0.000000, "TimeConnectSec":5.553587, 
"TimeNamelookupSec":5.097553, "TimePreTransferSec":5.553868, 
"TimeRedirectSec":0.000000, "TimeStartTransferSec":5.827584, 
"TimeTotalSec":5.827704, "UrlEffective":"http://dns" }

我得到兩個 JSON 輸出,一個用於 curl 信息,一個來自服務器。 如何將這兩個輸出組合成一個 JSON 變量? 請幫忙。

guid_id=$(uuidgen)
file_1="curlJsonRes_$guid_id.json"
file_2="curlMetaRes_$guid_id.json"

do_request(){
  echo $(curl $HOST_ADDR --silent --output $file_1 -w "\n{\n
         \"HttpCode\": %{http_code},\n
         \"NumRedirects\":%{num_redirects},\n
         \"NumConnects\":%{num_connects},\n
    }")
} > $file_2

do_request
#Merging file outputs
echo $(jq -s '.[0] * .[1]' $file_1 $file_2) > $RESULTS_FILE
rm $file_1
rm $file_2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM