Skip to content

Commit

Permalink
[Bash] Support post form data (#10795)
Browse files Browse the repository at this point in the history
* curl with form data

* fix type

* update bash readme

* typo

* update sample

* add test for form data

* update
  • Loading branch information
kevchentw authored Nov 9, 2021
1 parent 955aadc commit 7d092e7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ $ echo '<body_content>' | {{scriptName}} --host <hostname> --content-type json <
# }
$ echo '<body_content>' | {{scriptName}} --host <hostname> --content-type json <operationId> key1==value1 key2=value2 key3:=23 -

# Make POST request with form data
$ {{scriptName}} --host <hostname> <operationId> key1:=value1 key2:=value2 key3:=23

# Preview the cURL command without actually executing it
$ {{scriptName}} --host http://<hostname>:<port> --dry-run <operationid>

Expand Down
33 changes: 33 additions & 0 deletions modules/openapi-generator/src/main/resources/bash/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,29 @@ body_parameters_to_json() {
fi
}

##############################################################################
#
# Converts an associative array into form urlencoded string
#
##############################################################################
body_parameters_to_form_urlencoded() {
local body_form_urlencoded="-d '"
local count=0
for key in "${!body_parameters[@]}"; do
if [[ $((count++)) -gt 0 ]]; then
body_form_urlencoded+="&"
fi
body_form_urlencoded+="${key}=${body_parameters[${key}]}"
done
body_form_urlencoded+="'"

if [[ "${#body_parameters[@]}" -eq 0 ]]; then
echo ""
else
echo "${body_form_urlencoded}"
fi
}

##############################################################################
#
# Helper method for showing error because for example echo in
Expand Down Expand Up @@ -841,11 +864,21 @@ call_{{operationId}}() {
fi
{{/hasBodyParam}}
{{^hasBodyParam}}
{{#hasFormParams}}
body_form_urlencoded=$(body_parameters_to_form_urlencoded)
if [[ "$print_curl" = true ]]; then
echo "curl ${body_form_urlencoded} ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl ${body_form_urlencoded} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
{{/hasFormParams}}
{{^hasFormParams}}
if [[ "$print_curl" = true ]]; then
echo "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
{{/hasFormParams}}
{{/hasBodyParam}}
}

Expand Down
3 changes: 3 additions & 0 deletions samples/client/petstore/bash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ $ echo '<body_content>' | petstore-cli --host <hostname> --content-type json <op
# }
$ echo '<body_content>' | petstore-cli --host <hostname> --content-type json <operationId> key1==value1 key2=value2 key3:=23 -

# Make POST request with form data
$ petstore-cli --host <hostname> <operationId> key1:=value1 key2:=value2 key3:=23

# Preview the cURL command without actually executing it
$ petstore-cli --host http://<hostname>:<port> --dry-run <operationid>

Expand Down
53 changes: 41 additions & 12 deletions samples/client/petstore/bash/petstore-cli
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,29 @@ body_parameters_to_json() {
fi
}

##############################################################################
#
# Converts an associative array into form urlencoded string
#
##############################################################################
body_parameters_to_form_urlencoded() {
local body_form_urlencoded="-d '"
local count=0
for key in "${!body_parameters[@]}"; do
if [[ $((count++)) -gt 0 ]]; then
body_form_urlencoded+="&"
fi
body_form_urlencoded+="${key}=${body_parameters[${key}]}"
done
body_form_urlencoded+="'"

if [[ "${#body_parameters[@]}" -eq 0 ]]; then
echo ""
else
echo "${body_form_urlencoded}"
fi
}

##############################################################################
#
# Helper method for showing error because for example echo in
Expand Down Expand Up @@ -2286,10 +2309,11 @@ call_testEndpointParameters() {
if [[ -n $basic_auth_credential ]]; then
basic_auth_option="-u ${basic_auth_credential}"
fi
body_form_urlencoded=$(body_parameters_to_form_urlencoded)
if [[ "$print_curl" = true ]]; then
echo "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
echo "curl ${body_form_urlencoded} ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
eval "curl ${body_form_urlencoded} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
}

Expand Down Expand Up @@ -2322,10 +2346,11 @@ call_testEnumParameters() {
if [[ -n $basic_auth_credential ]]; then
basic_auth_option="-u ${basic_auth_credential}"
fi
body_form_urlencoded=$(body_parameters_to_form_urlencoded)
if [[ "$print_curl" = true ]]; then
echo "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
echo "curl ${body_form_urlencoded} ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
eval "curl ${body_form_urlencoded} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
}

Expand Down Expand Up @@ -2472,10 +2497,11 @@ call_testJsonFormData() {
if [[ -n $basic_auth_credential ]]; then
basic_auth_option="-u ${basic_auth_credential}"
fi
body_form_urlencoded=$(body_parameters_to_form_urlencoded)
if [[ "$print_curl" = true ]]; then
echo "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
echo "curl ${body_form_urlencoded} ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
eval "curl ${body_form_urlencoded} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
}

Expand Down Expand Up @@ -2918,10 +2944,11 @@ call_updatePetWithForm() {
if [[ -n $basic_auth_credential ]]; then
basic_auth_option="-u ${basic_auth_credential}"
fi
body_form_urlencoded=$(body_parameters_to_form_urlencoded)
if [[ "$print_curl" = true ]]; then
echo "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
echo "curl ${body_form_urlencoded} ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
eval "curl ${body_form_urlencoded} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
}

Expand Down Expand Up @@ -2954,10 +2981,11 @@ call_uploadFile() {
if [[ -n $basic_auth_credential ]]; then
basic_auth_option="-u ${basic_auth_credential}"
fi
body_form_urlencoded=$(body_parameters_to_form_urlencoded)
if [[ "$print_curl" = true ]]; then
echo "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
echo "curl ${body_form_urlencoded} ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
eval "curl ${body_form_urlencoded} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
}

Expand Down Expand Up @@ -2990,10 +3018,11 @@ call_uploadFileWithRequiredFile() {
if [[ -n $basic_auth_credential ]]; then
basic_auth_option="-u ${basic_auth_credential}"
fi
body_form_urlencoded=$(body_parameters_to_form_urlencoded)
if [[ "$print_curl" = true ]]; then
echo "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
echo "curl ${body_form_urlencoded} ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
else
eval "curl -d '' ${basic_auth_option} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
eval "curl ${body_form_urlencoded} ${curl_arguments} ${headers_curl} -X ${method} \"${host}${path}\""
fi
}

Expand Down
7 changes: 7 additions & 0 deletions samples/client/petstore/bash/tests/petstore_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,10 @@ export PETSTORE_HOST="http://petstore.swagger.io"
bash $PETSTORE_CLI -ct json -ac xml addPet -"
[[ "$output" =~ "<id>37567</id>" ]]
}

@test "updatePet with form data" {
run bash $PETSTORE_CLI --dry-run \
updatePetWithForm petId=1 name:=lucky status:=available
[[ "$output" =~ "status=available&name=lucky" ]]
[[ "$output" =~ "/v2/pet/1" ]]
}

0 comments on commit 7d092e7

Please sign in to comment.