【发布时间】:2021-01-25 09:03:24
【问题描述】:
说实话,我很困惑——我以为我现在已经了解了 bash 的基础知识,但事实并非如此。
问题:我想使用如下所示的 curl 包装器向 api 发出 POST 请求,但是当我使用包装器时,没有任何反应
包装器在其他情况下也能正常工作(即 ${_CURL} -s -o /dev/null -w %{http_code} ${URL})
IDEA1:所以我认为这可能与 json 对象周围的引号有关,我过去遇到过问题,所以我最终插入了“'${JSON}'”,哪个没用
IDEA2:在 curl 包装器中,我将所有参数 ("-b ${COOKIES} $@") 放在引号中,但什么也没有
卷曲包装
#!/bin/bash
# sets headers to use cookies
# ${Cookies} is set and translates to an absolute path of ./cookie-file.txt
if [ -f "${COOKIES}" ]; then
curl -b "${COOKIES}" $@
else
curl $@
fi
Json 对象,我认为它很重要
JSON=$( jq -n -r \
--arg login $LOGIN \
--arg pass ${PASS_INPUTS[@]} \
'{username: $login, password: $pass}')
文件id用在中
#JSON is located right above the following line
#_CURL being the path to the wrapper i exported
# Does not even send a request to the URL at $V2/auth/login
RESPONSE=$($_CURL -s \
--header "Content-Type: application/json" \
--request POST \
--data "$JSON" \
$ARGS \
"${V2}/auth/login")
echo $RESPONSE
#---------------------------------------------------------------------------------------------
# This is what i had previously but Id rather not
# works just fine
[ ! -f "cookie-file.txt" ] && ARGS="-c cookie-file.txt" || ARGS="-b cookie-file.txt"
RESPONSE=$(curl -s \
--header "Content-Type: application/json" \
--request POST \
--data "$JSON" \
$ARGS \
"${V2}/auth/login")
echo $RESPONSE
【问题讨论】:
-
ShellCheck自动检测this等常见问题
-
之前听说过,但我们在这里 - 在低生活下不是最聪明的@thatotherguy