Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save venkata-qa/4e92a1dda66cf338454f709cf6811001 to your computer and use it in GitHub Desktop.
Save venkata-qa/4e92a1dda66cf338454f709cf6811001 to your computer and use it in GitHub Desktop.
Curl - Get status code and response body
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