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.

Revisions

  1. venkata-qa revised this gist Jan 6, 2021. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions curl-get-status-code-and-response-body.sh
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,22 @@
    URL="http://stackoverflow.com/"
    GET_USERS_URL="https://reqres.in/api/users?page=2"

    # store the whole response with the status at the and
    HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $URL)
    # Store the status code and the response
    HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X POST $GET_USERS_URL)

    # extract the body
    # Extract the response body
    HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')

    # extract the status
    # Extract the status code
    HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')

    # print the body
    echo "$HTTP_BODY"
    # Print the response body
    echo "API response code: $HTTP_STATUS"

    # example using the status
    if [ ! $HTTP_STATUS -eq 200 ]; then
    # 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
  2. @maxcnunes maxcnunes created this gist Nov 24, 2015.
    19 changes: 19 additions & 0 deletions curl-get-status-code-and-response-body.sh
    Original 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