Forked from maxclaus/curl-get-status-code-and-response-body.sh
Last active
January 6, 2021 22:11
-
-
Save venkata-qa/4e92a1dda66cf338454f709cf6811001 to your computer and use it in GitHub Desktop.
Revisions
-
venkata-qa revised this gist
Jan 6, 2021 . 1 changed file with 12 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,22 @@ GET_USERS_URL="https://reqres.in/api/users?page=2" # Store the status code and the response HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $GET_USERS_URL) # Extract the response body HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g') # Extract the status code HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') # Print the response body echo "API response code: $HTTP_STATUS" # Print the response body echo "API response body: $HTTP_BODY" # Assert the status code if [ ! $HTTP_STATUS -eq 201 ]; then echo "Error [HTTP status: $HTTP_STATUS]" exit 1 fi -
maxcnunes created this gist
Nov 24, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ 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