kdt*_*kdt 1104 http curl status
我curl在 Linux 上的命令行中使用来发出 HTTP 请求。响应正文打印为标准输出,这很好,但我从手册页看不到如何让 curl 从响应(404、403 等)打印 HTTP 状态代码。这可能吗?
小智 1165
打印出更具体的方式只是HTTP状态代码是沿着线的东西:
curl -s -o /dev/null -w "%{http_code}" http://www.example.org/
Run Code Online (Sandbox Code Playgroud)
在脚本中更容易使用,因为它不需要任何解析:-)
-I可能会添加该参数以提高响应负载性能。这会将调用更改为HEAD仅获取响应开销的调用,而没有主体。
注意: %{http_code}在 HTTP 负载的第一行返回
IE:
curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/
Run Code Online (Sandbox Code Playgroud)
小智 743
如果Web 服务器能够响应 HEAD 请求(这不会执行GET),这应该对您有用:
curl -I http://www.example.org
Run Code Online (Sandbox Code Playgroud)
另外,要让 cURL 跟随重定向(3xx 状态),请添加 -L。
小智 345
除了所有标题外,您还可以通过执行以下操作打印状态代码:
curl -i http://example.org
Run Code Online (Sandbox Code Playgroud)
好处-i是它也适用-X POST。
Enr*_*tyo 264
如果您想查看标题和结果,您可以使用详细选项:
curl -v http://www.example.org
curl --verbose http://www.example.org
Run Code Online (Sandbox Code Playgroud)
状态将出现在标题中。例如
< Date: Tue, 04 Nov 2014 19:12:59 GMT
< Content-Type: application/json; charset=utf-8
< Status: 422 Unprocessable Entity
Run Code Online (Sandbox Code Playgroud)
Hea*_*ers 84
如果要在变量中捕获 HTTP 状态代码,但仍将内容重定向到 STDOUT,则必须创建两个 STDOUT。您可以使用进程替换 >()和命令替换 $() 来实现。
首先,3为当前进程的 STDOUT创建一个文件描述符exec 3>&1。
然后,用卷曲的-o选项,以响应内容到一个临时重定向FIFO使用命令替换,然后将该命令替换中,重定向输出回到你的当前进程STDOUT文件描述符3用-o >(cat >&3)。
将它们放在一起bash 3.2.57(1)-release(标准为macOS):
# creates a new file descriptor 3 that redirects to 1 (STDOUT)
exec 3>&1
# Run curl in a separate command, capturing output of -w "%{http_code}" into HTTP_STATUS
# and sending the content to this command's STDOUT with -o >(cat >&3)
HTTP_STATUS=$(curl -w "%{http_code}" -o >(cat >&3) 'http://example.com')
Run Code Online (Sandbox Code Playgroud)
请注意,/bin/sh正如SamK 在下面的评论中指出的那样,这不起作用。
小智 41
重新定义 curl 输出:
curl -sw '%{http_code}' http://example.org
Run Code Online (Sandbox Code Playgroud)
可用于任何请求类型。
mah*_*ich 33
仅状态代码
[0]$ curl -LI http://www.example.org -o /dev/null -w '%{http_code}\n' -s
[0]$ 200
Run Code Online (Sandbox Code Playgroud)
归功于此GIST
Luc*_*mon 18
这是一个痛苦的curl --fail限制。来自man curl:
-f, --fail (HTTP) 服务器错误时静默失败(根本没有输出)
但是没有办法在标准输出中同时获得非零返回码和响应正文。
基于pvandenberk的回答和在 SO 上学到的另一个非常有用的技巧,这里有一个解决方法:
curl_with_error_code () {
_curl_with_error_code "$@" | sed '$d'
}
_curl_with_error_code () {
local curl_error_code http_code
exec 17>&1
http_code=$(curl --write-out '\n%{http_code}\n' "$@" | tee /dev/fd/17 | tail -n 1)
curl_error_code=$?
exec 17>&-
if [ $curl_error_code -ne 0 ]; then
return $curl_error_code
fi
if [ $http_code -ge 400 ] && [ $http_code -lt 600 ]; then
echo "HTTP $http_code" >&2
return 127
fi
}
Run Code Online (Sandbox Code Playgroud)
此函数的行为与 完全相同curl,但curl如果 HTTP 代码在 [400, 600[.
Fil*_*nov 13
这将向 url 发送请求,仅获取响应的第一行,将其拆分为块并选择第二行。
它包含响应代码
curl -I http://example.org 2>/dev/null | head -n 1 | cut -d$' ' -f2
Run Code Online (Sandbox Code Playgroud)
小智 12
对于 POST 请求,以下操作有效:
curl -w 'RESP_CODE:%{response_code}' -s -X POST --data '{"asda":"asd"}' http://example.com --header "Content-Type:application/json"|grep -o 'RESP_CODE:[1-4][0-9][0-9]'
Run Code Online (Sandbox Code Playgroud)
如何使用响应代码的示例。-z仅当 Geolite 数据库已更改 ( ) 且遵循重定向 ( -L)时,我才用它来重新下载它们:
url=http://example.com/file.gz
file=$(basename $url)
response=$(curl -L -s -o $file -z $file $url -w "%{http_code}")
case "$response" in
200) do_something ;;
301) do_something ;;
304) printf "Received: HTTP $response (file unchanged) ==> $url\n" ;;
404) printf "Received: HTTP $response (file not found) ==> $url\n" ;;
*) printf "Received: HTTP $response ==> $url\n" ;;
esac
Run Code Online (Sandbox Code Playgroud)
小智 6
使用以下 cURL 命令并将其通过管道传递给 grep,如下所示:
$ curl -I -s -L http://example.com/v3/get_list | grep "HTTP/1.1"
这是每个标志的作用:
-I: 只显示响应头-s: Silent - 不显示进度条-L: 按照Location:标题这是指向HTTP 状态代码的链接。
从命令行运行。此 curl 以静默模式运行,遵循任何重定向,获取 HTTP 标头。grep 会将 HTTP 状态代码打印到标准输出。
这是一些正在使用GET并返回 HTTP 代码的curl 命令。
curl -so /dev/null -w '%{response_code}' http://www.example.org
Run Code Online (Sandbox Code Playgroud)
请记住,下面的方法使用的是HEAD,它速度更快,但它可能不适用于某些 Web 不兼容的 HTTP 服务器。
curl -I http://www.example.org
Run Code Online (Sandbox Code Playgroud)
curl -so -i /dev/null -w "%{http_code}" http://www.any_example.com
Run Code Online (Sandbox Code Playgroud)
这将返回以下信息:
将输出内容拆分为stdout并将 HTTP 状态代码拆分为stderr:
curl http://www.example.org -o >(cat >&1) -w "%{http_code}\n" 1>&2
Run Code Online (Sandbox Code Playgroud)
如果仅需要 HTTP 状态代码作为 stderr,--silent可以使用:
curl --silent http://www.example.org -o >(cat >&1) -w "%{http_code}\n" 1>&2
Run Code Online (Sandbox Code Playgroud)
然后可以通过将不需要的流重定向到来选择所需的流/dev/null:
$ (curl --silent http://www.example.org -o >(cat >&1) -w "%{http_code}" 1>&2) 1>/dev/null
200
$ (curl --silent http://www.example.org -o >(cat >&1) -w "%{http_code}" 1>&2) 2>/dev/null
<!doctype html>
...
Run Code Online (Sandbox Code Playgroud)
请注意,为了使第二次重定向按预期运行,我们需要在子 shell 中运行curl 命令。