我使用curl来获取http报头以查找http状态代码并返回响应。我使用命令获取http头信息

curl -I http://localhost

为了得到响应,我使用命令

curl http://localhost

一旦使用了-I标志,我就只得到了头信息,响应就不再存在了。是否有一种方法可以同时获得http响应和头/http状态码在一个命令?


当前回答

一个清晰的使用管道读取

function cg(){
    curl -I --silent www.google.com | head -n 1 | awk -F' ' '{print $2}'
}
cg
# 200

欢迎在这里使用我的dotfile脚本

解释

——silent:使用管道时不显示进度条 head -n 1:只显示第一行 -F' ':使用分隔符分隔文本 '{print $2}':显示第二列

其他回答

对于编程使用,我使用以下代码:

curlwithcode() {
    code=0
    # Run curl in a separate command, capturing output of -w "%{http_code}" into statuscode
    # and sending the content to a file with -o >(cat >/tmp/curl_body)
    statuscode=$(curl -w "%{http_code}" \
        -o >(cat >/tmp/curl_body) \
        "$@"
    ) || code="$?"

    body="$(cat /tmp/curl_body)"
    echo "statuscode : $statuscode"
    echo "exitcode : $code"
    echo "body : $body"
}

curlwithcode https://api.github.com/users/tj

显示如下信息:

statuscode : 200
exitcode : 0
body : {
  "login": "tj",
  "id": 25254,
  ...
}

详细模式会告诉你一切

curl -v http://localhost

哇,这么多答案,cURL开发人员肯定把它留给了我们作为家庭练习:)好吧,这是我的想法-一个脚本,使cURL工作,因为它应该是,即:

像cURL那样显示输出。 如果HTTP响应代码不在2XX范围内,则使用非零代码退出

保存为curl-wrapper.sh:


#!/bin/bash

output=$(curl -w "\n%{http_code}" "$@")
res=$?

if [[ "$res" != "0" ]]; then
  echo -e "$output"
  exit $res
fi

if [[ $output =~ [^0-9]([0-9]+)$ ]]; then
    httpCode=${BASH_REMATCH[1]}
    body=${output:0:-${#httpCode}}

    echo -e "$body"

    if (($httpCode < 200 || $httpCode >= 300)); then
        # Remove this is you want to have pure output even in 
        # case of failure:
        echo
        echo "Failure HTTP response code: ${httpCode}"
        exit 1
    fi
else
    echo -e "$output"
    echo
    echo "Cannot get the HTTP return code"
    exit 1
fi

所以它就像往常一样,但不是curl do ./curl-wrapper.sh:

所以当结果在200-299范围内时:

./curl-wrapper.sh www.google.com 
# ...the same output as pure curl would return...
echo $?
# 0

当结果超出200-299范围时:

./curl-wrapper.sh www.google.com/no-such-page
# ...the same output as pure curl would return - plus the line
#    below with the failed HTTP code, this line can be removed if needed:
#
# Failure HTTP response code: 404
echo $?
# 1

只是不要传递“-w|——write-out”参数,因为这是脚本中添加的内容

我的方法是:

为了获得两者(标题和正文),我通常执行curl - d - <url>,如:

$ curl -D- http://localhost:1234/foo
HTTP/1.1 200 OK
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
Date: Wed, 29 Jul 2020 20:59:21 GMT

{"data":["out.csv"]}

这将转储标题(- d)到stdout(-)(在man curl中查找——dump-header)。

恕我直言,在这种情况下也很方便:

我经常使用jq来获得json数据(例如从一些其他api)格式化。但是由于jq不需要HTTP标头,诀窍是使用-D/dev/stderr将标头打印到stderr。注意,这次我们还使用-sS(——silent,——show-errors)来抑制进度表(因为我们写入管道)。

$ curl -sSD/dev/stderr http://localhost:1231/foo | jq .
HTTP/1.1 200 OK
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
Date: Wed, 29 Jul 2020 21:08:22 GMT

{
  "data": [
    "out.csv"
  ]
}

我想这也可以很方便,如果你想打印头部(快速检查)控制台,但重定向主体到一个文件(例如,当它的某种二进制不混乱你的终端):

$ curl -sSD/dev/stderr http://localhost:1231 > /dev/null
HTTP/1.1 200 OK
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: application/json
Date: Wed, 29 Jul 2020 21:20:02 GMT

注意:这与curl -I <url>!As -I将执行HEAD请求而不是GET请求(在man curl中查找——HEAD。是:对于大多数HTTP服务器,这将产生相同的结果。但是我知道很多商业应用程序根本没有实现HEAD请求;-P

这对我来说很管用:

curl -Uri 'google.com' | select-object StatusCode