From 8cc1d2f16487187cdf749e61de2e30e6026a6760 Mon Sep 17 00:00:00 2001 From: Juan Manuel Lopez Date: Wed, 2 Oct 2024 15:53:34 -0300 Subject: [PATCH] =?utf8?q?Funci=C3=B3n=20para=20llamar=20al=20comando=20`c?= =?utf8?q?url`?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- openapi/test-api.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/openapi/test-api.sh b/openapi/test-api.sh index d6787f4..860387a 100755 --- a/openapi/test-api.sh +++ b/openapi/test-api.sh @@ -75,3 +75,50 @@ function msg { echo ">>>>>>>>>>>> ${message}" fi } + +# -------------------------------------- +# Call cURL command +# +# Use following global variables to return/export values: +# +# - CURL_EXIT_CODE +# - CURL_RESPONSE_HEADER +# - CURL_RESPONSE_DATA +# +# @param string $1 Path +# @param string $2 Method +# @param string $3 Data +# -------------------------------------- +function callRestApi { + CURL_EXIT_CODE=1 + CURL_RESPONSE_HEADER='' + CURL_RESPONSE_DATA='' + + local path="${1}" + local method="${2:-GET}" + local data="${3}" + + if [[ ! -z "${data}" ]]; then + data="--data ${data}" + fi + + curl "${HOST}:${PORT}${path}" \ + --request "${method}" \ + --header "Content-Type: application/json" \ + --header "Accept: application/json" \ + --dump-header curl_response_header.txt \ + --output curl_response_data.txt \ + $data &> /dev/null + + CURL_EXIT_CODE=$? + + if [ -f curl_response_header.txt ]; then + CURL_RESPONSE_HEADER=$(cat curl_response_header.txt) + rm curl_response_header.txt + fi + + if [ -f curl_response_data.txt ]; then + CURL_RESPONSE_DATA=$(cat curl_response_data.txt) + rm curl_response_data.txt + fi +} -- 2.11.4.GIT