Forked from maxclaus/curl-get-status-code-and-response-body.sh
Created
August 4, 2017 05:30
-
-
Save fengdi721/4a91d73ed9119b7d111d8deae14ac9f7 to your computer and use it in GitHub Desktop.
Curl - Get status code and response body
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| URL="http://stackoverflow.com/" | |
| # store the whole response with the status at the and | |
| HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $URL) | |
| # extract the body | |
| HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g') | |
| # extract the status | |
| HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') | |
| # print the body | |
| echo "$HTTP_BODY" | |
| # example using the status | |
| if [ ! $HTTP_STATUS -eq 200 ]; then | |
| echo "Error [HTTP status: $HTTP_STATUS]" | |
| exit 1 | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment