连接 2 个变量以放入 json 无效
Cocatenate 2 variables to put inside json is not working
我需要连接 2 个变量,但是当我执行 bash 代码时出现此错误 filters.api.malformed_request_body
。
我需要获取当前 IP,以便能够向过滤器添加新 IP,那是因为我需要使用两个变量,$a
是我防火墙规则中的当前 IP, $b
是我要添加的新 IP。
来自 Cloudflare
To preserve existing values, issue a GET request and based on the response, determine which fields (and respective values) to include in your PUT request and that way, avoid any undesired overwrites.
代码:
a=122.16.89.10
b=137.77.77.77
curl -X PUT \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: KEY" \
-H "Content-Type: application/json" \
-d '[
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {'$a'" "'$b'})",
"description": "Block IP"
}
]' "https://api.cloudflare.com/client/v4/zones/ZONE/filters"
我也尝试过:"(ip.src in {'$a $b'})"
和:
new_filter="$a $b"
...
...
"(ip.src in {'$new_filter'})"
如果我 echo $new_filter
它显示正确的结果:
new_filter="$a $b"
echo $new_filter
#122.16.89.10 137.77.77.77
当我使用变量 $new_filter
时它也显示这个错误 curl: (3) [globbing] unmatched close brace/bracket in column 13
第 13 行是这个 -H "Content-Type: application/json" \
.
None 有效,为什么?我收到此错误:
{
"result": null,
"success": false,
"errors": [
{
"code": 10014,
"message": "filters.api.malformed_request_body"
}
],
"messages": []
}
这有效:"(ip.src in {'$a'})"
。
好吧,让我们以您的第一个示例为例,将其修改为打印 JSON 正文:
export a=122.16.89.10
export b=137.77.77.77
echo '[
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {'$a'" "'$b'})",
"description": "Block IP"
}
]'
输出是这样的:
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {122.16.89.10" "137.77.77.77})",
"description": "Block IP"
}
]
可以看到JSON是无效的。 expression
.
中的双引号不平衡
改为尝试 "expression": "(ip.src in {'$a' '$b'})",
-- 这将产生有效的 JSON.
我需要连接 2 个变量,但是当我执行 bash 代码时出现此错误 filters.api.malformed_request_body
。
我需要获取当前 IP,以便能够向过滤器添加新 IP,那是因为我需要使用两个变量,$a
是我防火墙规则中的当前 IP, $b
是我要添加的新 IP。
来自 Cloudflare
To preserve existing values, issue a GET request and based on the response, determine which fields (and respective values) to include in your PUT request and that way, avoid any undesired overwrites.
代码:
a=122.16.89.10
b=137.77.77.77
curl -X PUT \
-H "X-Auth-Email: EMAIL" \
-H "X-Auth-Key: KEY" \
-H "Content-Type: application/json" \
-d '[
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {'$a'" "'$b'})",
"description": "Block IP"
}
]' "https://api.cloudflare.com/client/v4/zones/ZONE/filters"
我也尝试过:"(ip.src in {'$a $b'})"
和:
new_filter="$a $b"
...
...
"(ip.src in {'$new_filter'})"
如果我 echo $new_filter
它显示正确的结果:
new_filter="$a $b"
echo $new_filter
#122.16.89.10 137.77.77.77
当我使用变量 $new_filter
时它也显示这个错误 curl: (3) [globbing] unmatched close brace/bracket in column 13
第 13 行是这个 -H "Content-Type: application/json" \
.
None 有效,为什么?我收到此错误:
{
"result": null,
"success": false,
"errors": [
{
"code": 10014,
"message": "filters.api.malformed_request_body"
}
],
"messages": []
}
这有效:"(ip.src in {'$a'})"
。
好吧,让我们以您的第一个示例为例,将其修改为打印 JSON 正文:
export a=122.16.89.10
export b=137.77.77.77
echo '[
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {'$a'" "'$b'})",
"description": "Block IP"
}
]'
输出是这样的:
{
"id": "ID",
"paused": false,
"expression": "(ip.src in {122.16.89.10" "137.77.77.77})",
"description": "Block IP"
}
]
可以看到JSON是无效的。 expression
.
改为尝试 "expression": "(ip.src in {'$a' '$b'})",
-- 这将产生有效的 JSON.