如何在 bash 脚本中使用 HTTPie 捕获实际的响应代码和响应主体?
How to capture actual response code and response body with HTTPie in bash script?
我有一个 bash 脚本可以使用 HTTPie 调用多个 API。我想捕获响应正文和 HTTP 状态代码。
这是迄今为止我管理的最好的:
rspBody=$( http $URL --check-status --ignore-stdin )
statusCode=$?
命令替换让我得到正文,“--check-status”标志给了我一个与代码族相对应的简化代码(例如 0、3、4 等)。
问题是我需要区分 401 和 404 代码,但我只得到 4。
有没有一种方法可以获取实际的状态代码,而不必将详细的转储到文件中并解析内容?
[编辑]
这是我的解决方法,以防对任何人有帮助,但如果您有更好的主意,我仍然想要一个更好的主意:
TMP=$(mktemp)
FLUSH_RSP=$( http POST ${CACHE_URL} --check-status --ignore-stdin 2> "$TMP")
STAT_FAMILY=$?
flush_err=$(cat "$TMP" | awk '{
where = match([=12=], /[0-9]+/)
if (where) {
print substr([=12=], RSTART, RLENGTH);
}
}' -)
rm "$TMP"
STDERR 包含一个(通常)3 行消息,其中包含 HTTP 代码,因此我将其转储到一个临时文件中,并且仍然能够在变量中捕获响应正文(来自 STDOUT)。
然后我解析该临时文件以查找数字,但这对我来说似乎很脆弱。
对此没有现成的解决方案,但可以通过编写一些脚本来实现。例如:
STATUS=$(http -hdo ./body httpbin.org/get 2>&1 | grep HTTP/ | cut -d ' ' -f 2)
BODY=$(cat ./body)
rm ./body
echo $STATUS
# 200
echo $BODY
# { "args": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "identity", "Host": "httpbin.org", "User-Agent": "HTTPie/1.0.0-dev" }, "origin": "84.242.118.58", "url": "http://httpbin.org/get" }
命令解释:
http --headers \ # Print out the response headers (they would otherwise be supressed for piped ouput)
--download \ # Enable download mode
--output=./body \ # Save response body to this file
httpbin.org/get 2>&1 \ # Merge STDERR into STDOUT (with --download, console output otherwise goes to STDERR)
| grep HTTP/ \ # Find the Status-Line
| cut -d ' ' -f 2 # Get the status code
https://gist.github.com/jakubroztocil/ad06f159c5afbe278b5fcfa8bf3b5313
我有一个 bash 脚本可以使用 HTTPie 调用多个 API。我想捕获响应正文和 HTTP 状态代码。
这是迄今为止我管理的最好的:
rspBody=$( http $URL --check-status --ignore-stdin )
statusCode=$?
命令替换让我得到正文,“--check-status”标志给了我一个与代码族相对应的简化代码(例如 0、3、4 等)。
问题是我需要区分 401 和 404 代码,但我只得到 4。
有没有一种方法可以获取实际的状态代码,而不必将详细的转储到文件中并解析内容?
[编辑]
这是我的解决方法,以防对任何人有帮助,但如果您有更好的主意,我仍然想要一个更好的主意:
TMP=$(mktemp)
FLUSH_RSP=$( http POST ${CACHE_URL} --check-status --ignore-stdin 2> "$TMP")
STAT_FAMILY=$?
flush_err=$(cat "$TMP" | awk '{
where = match([=12=], /[0-9]+/)
if (where) {
print substr([=12=], RSTART, RLENGTH);
}
}' -)
rm "$TMP"
STDERR 包含一个(通常)3 行消息,其中包含 HTTP 代码,因此我将其转储到一个临时文件中,并且仍然能够在变量中捕获响应正文(来自 STDOUT)。
然后我解析该临时文件以查找数字,但这对我来说似乎很脆弱。
对此没有现成的解决方案,但可以通过编写一些脚本来实现。例如:
STATUS=$(http -hdo ./body httpbin.org/get 2>&1 | grep HTTP/ | cut -d ' ' -f 2)
BODY=$(cat ./body)
rm ./body
echo $STATUS
# 200
echo $BODY
# { "args": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "identity", "Host": "httpbin.org", "User-Agent": "HTTPie/1.0.0-dev" }, "origin": "84.242.118.58", "url": "http://httpbin.org/get" }
命令解释:
http --headers \ # Print out the response headers (they would otherwise be supressed for piped ouput)
--download \ # Enable download mode
--output=./body \ # Save response body to this file
httpbin.org/get 2>&1 \ # Merge STDERR into STDOUT (with --download, console output otherwise goes to STDERR)
| grep HTTP/ \ # Find the Status-Line
| cut -d ' ' -f 2 # Get the status code
https://gist.github.com/jakubroztocil/ad06f159c5afbe278b5fcfa8bf3b5313