簡體   English   中英

如何在 GraphQL cURL POST 請求的 Z0ECD11C1D7A287401D148AA2F 主體中插入 bash 變量

[英]How to insert a bash variable within GraphQL cURL POST request's JSON body

我一直在花費數小時嘗試格式化我的 json 帖子正文,以便我的腳本很開心,但我沒有任何運氣。 在下面的代碼中,我想使用 $utc_format 變量作為 datetime_gt 鍵的值。

#!/bin/bash

utc_format=$(date -v-3d -v-5M -u +"%Y-%m-%dT%H:%M:%SZ")

curl \
    -X POST \
    -H "Content-Type: application/json" \
    -H "X-Auth-Email: email@email.com" \
    -H "X-Auth-key: $API_KEY" \
    --data '{"query": "query { viewer { zones ( filter: { zoneTag_in: [ \"abcde12345\" ] } ) { firewallEventsAdaptive ( filter: { source: \"waf\" datetime_gt: \"$utc_format\" ruleId: \"123456\" action: \"simulate\" } orderBy: [ datetime_DESC ] limit: 100 ) { clientIP edgeResponseStatus metadata { key value } } } } }"}' \
    https://api.api.com/ \
    | python -m json.tool >> curl_results

我什至嘗試做類似的事情:

generate_post_data()
{
cat <<EOF
{
<json data>
}
EOF
}

但我有一個錯誤說

"message": "failed to recognize JSON request: 'invalid character '\\n' in string literal'"

:'(

先感謝您!

該消息的字面意思是您的 json 正文中有換行符。 嘗試刪除換行符,看看是否有幫助。

curl \
    -X POST \
    -H "Content-Type: application/json" \
    -H "X-Auth-Email: email@email.com" \
    -H "X-Auth-key: $API_KEY" \
    --data "$(generate_post_data | tr -d '\n')" \
    https://api.api.com/ \
    | python -m json.tool >> curl_results

暫無
暫無

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

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