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.
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
| 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment