传递一个标题数组以卷曲为bash脚本 [英] pass an array of headers to curl into a bash script
问题描述
我正在开发一个脚本,它是一个测试套件,可以使用curl检查所有无效的标头大小写。
I am working on a script which is a test suite that would check all invalid headers case using curl.
我创建了一堆函数,使我可以构造标头元组(字段名称:field_value)具有默认测试值。
I created a bunch of functions which allows me to construct headers tupe (field_name: field_value) with default testing value.
build_foo_header() {
local FOO_KN=${1:-'X-App-Name'}
local FOO_KV=${2:-'MyApp'}
echo $FOO_KN: $FOO_KV
}
我想在函数中创建一些缺少标头或标头具有无效值的数组,以测试我的应用行为。
I would like to create some arrays in functions with either missing headers or headers with invalid values to test my app behavior.
所以我创建了一个示例函数:
So i created a sample function as it :
build_headers_invalid_X_App_Name_Value() {
HEADERS=()
# this array should contain all other required headers
HEADERS[0]=$(build_foo_header X-App-Name BadValue)
# HEADERS[1]= header that is required with default value
# and son on for all the remaning required headers
echo "${HEADERS[@]/#/-H}"
}
但是我无法卷曲如何将数组传递给卷曲?
However i can't get curl how to pass that array to curl ?
$(curl $(build_headers_invalid_X_App_Name_Value) myURl)
似乎要向HEADERS数组中的第一个头发送请求。
Seems to send a request to the first header in the HEADERS array.
我大约有10个必需的头,这就是为什么
I have approximatively 10 required headers that's why I would like to script it that way.
推荐答案
问题是通过回显 $ {HEADERS [@ ] /#/-H}
,您会丢失在 HEADERS
元素中出现的空格和用于分隔其中两个元素的空格之间的任何区别输出。
The problem is by echoing "${HEADERS[@]/#/-H}"
, you lose any distinction between whitespace that occurred in an element of HEADERS
and whitespace used to separate two elements in the output.
如果要使用函数,则只需设置一个全局数组,然后直接使用它即可。
If you want to use a function, you need to just set a global array, then use that directly. No command substitution is needed or useful.
build_foo_header() {
local FOO_KN=${1:-'X-App-Name'}
local FOO_KV=${2:-'MyApp'}
echo "$FOO_KN: $FOO_KV"
}
build_headers_invalid_X_App_Name_Value() {
HEADERS=()
# this array should contain all other required headers
HEADERS[0]=$(build_foo_header X-App-Name BadValue)
# HEADERS[1]= header that is required with default value
# and son on for all the remaning required headers
}
build_headers_invalid_X_App_Name_Value
curl "${HEADERS[@]/#/-H}" myUrl
这篇关于传递一个标题数组以卷曲为bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!