Curl to return http status code along with the res

2020-02-02 04:09发布

I use curl to get http headers to find http status code and also return response. I get the http headers with the command

curl -I http://localhost

To get the response, I use the command

curl http://localhost

As soon as use the -I flag, I get only the headers and the response is no longer there. Is there a way to get both the http response and the headers/http status code in in one command?

标签: shell curl
9条回答
SAY GOODBYE
2楼-- · 2020-02-02 04:47

I found this question because I wanted BOTH the response and the content in order to add some error handling for the user.

You can print the HTTP status code to std out and write the contents to another file.

curl -s -o response.txt -w "%{http_code}" http://example.com

This let's you use logic to decide if the response is worth processing.

http_response=$(curl -s -o response.txt -w "%{http_code}" http://example.com)
if [ $http_response != "200" ]; then
    # handle error
else
    echo "Server returned:"
    cat response.txt    
fi
查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-02 04:47

This command

 curl http://localhost -w ", %{http_code}"

will get the comma separated body and status; you can split them to get them out.

You can change the delimiter as you like.

查看更多
聊天终结者
4楼-- · 2020-02-02 04:52

For programmatic usage, I use the following :

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

It shows following output :

statuscode : 200
exitcode : 0
body : {
  "login": "tj",
  "id": 25254,
  ...
}
查看更多
Melony?
5楼-- · 2020-02-02 05:00

the verbose mode will tell you everything

curl -v http://localhost
查看更多
何必那么认真
6楼-- · 2020-02-02 05:01

I was able to get a solution by looking at the curl doc which specifies to use - for the output to get the output to stdout.

curl -o - http://localhost

To get the response with just the http return code, I could just do

curl -o /dev/null -s -w "%{http_code}\n" http://localhost
查看更多
【Aperson】
7楼-- · 2020-02-02 05:05

This is a way to retrieve the body "AND" the status code and format it to a proper json or whatever format works for you. Some may argue it's the incorrect use of write format option but this works for me when I need both body and status code in my scripts to check status code and relay back the responses from server.

curl -X GET \
 -w "%{stderr}{\"status\": \"%{http_code}\", \"body\":%{stdout}}"  -s -o - \
     "https://jsonplaceholder.typicode.com/posts?userId=1" 2>&1 | jq

run the code above and you should get back a json in this format:

{
"status" : <status code>,
"body" : <body of response>
}

with the -w write format option, since stderr is printed first, you can format your output with the var http_code and place the body of the response in a value (body) and follow up the enclosing using var stdout. Then redirect your stderr output to stdout and you'll be able to combine both http_code and response body into a neat output

查看更多
登录 后发表回答