diff --git a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache index 5c364247594..2435b7b0ced 100644 --- a/modules/openapi-generator/src/main/resources/powershell/api_client.mustache +++ b/modules/openapi-generator/src/main/resources/powershell/api_client.mustache @@ -56,9 +56,15 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $HeaderParameters['Accept'] = $Accept } + [string]$MultiPartBoundary = $null $ContentType= SelectHeaders -Headers $ContentTypes if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType + if ($ContentType -eq 'multipart/form-data') { + [string]$MultiPartBoundary = [System.Guid]::NewGuid() + $MultiPartBoundary = "---------------------------$MultiPartBoundary" + $HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary" + } } # add default headers if any @@ -66,7 +72,6 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { $HeaderParameters[$header.Name] = $header.Value } - # construct URL query string $HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) foreach ($Parameter in $QueryParameters.GetEnumerator()) { @@ -84,9 +89,34 @@ function Invoke-{{{apiNamePrefix}}}ApiClient { # include form parameters in the request body if ($FormParameters -and $FormParameters.Count -gt 0) { - $RequestBody = $FormParameters + if (![string]::IsNullOrEmpty($MultiPartBoundary)) { + $RequestBody = "" + $LF = "`r`n" + $FormParameters.Keys | ForEach-Object { + $value = $FormParameters[$_] + $isFile = $value.GetType().FullName -eq "System.IO.FileInfo" + + $RequestBody += "--$MultiPartBoundary$LF" + $RequestBody += "Content-Disposition: form-data; name=`"$_`"" + if ($isFile) { + $fileName = $value.Name + $RequestBody += "; filename=`"$fileName`"$LF" + $RequestBody += "Content-Type: application/octet-stream$LF$LF" + $RequestBody += Get-Content -Path $value.FullName + } else { + $RequestBody += "$LF$LF" + $RequestBody += ([string]$value) + } + $RequestBody += "$LF--$MultiPartBoundary" + } + $RequestBody += "--" + } else { + $RequestBody = $FormParameters + } } + + if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) { diff --git a/samples/client/petstore/powershell/src/PSPetstore/PSPetstore.psd1 b/samples/client/petstore/powershell/src/PSPetstore/PSPetstore.psd1 index 6329693a91f..2860418807d 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/PSPetstore.psd1 +++ b/samples/client/petstore/powershell/src/PSPetstore/PSPetstore.psd1 @@ -3,7 +3,7 @@ # # Generated by: OpenAPI Generator Team # -# Generated on: 30-11-2020 +# Generated on: 10/30/2021 # @{ @@ -45,7 +45,7 @@ PowerShellVersion = '5.0' # DotNetFrameworkVersion = '' # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. -# CLRVersion = '' +# ClrVersion = '' # Processor architecture (None, X86, Amd64) required by this module # ProcessorArchitecture = '' @@ -128,6 +128,15 @@ PrivateData = @{ # ReleaseNotes of this module ReleaseNotes = 'This is a sample project' + # Prerelease string of this module + # Prerelease = '' + + # Flag to indicate whether the module requires explicit user acceptance for install/update/save + # RequireLicenseAcceptance = $false + + # External dependent modules of this module + # ExternalModuleDependencies = @() + } # End of PSData hashtable } # End of PrivateData hashtable diff --git a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 index aacf6f6d0d1..11745f18614 100644 --- a/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 +++ b/samples/client/petstore/powershell/src/PSPetstore/Private/PSApiClient.ps1 @@ -62,9 +62,15 @@ function Invoke-PSApiClient { $HeaderParameters['Accept'] = $Accept } + [string]$MultiPartBoundary = $null $ContentType= SelectHeaders -Headers $ContentTypes if ($ContentType) { $HeaderParameters['Content-Type'] = $ContentType + if ($ContentType -eq 'multipart/form-data') { + [string]$MultiPartBoundary = [System.Guid]::NewGuid() + $MultiPartBoundary = "---------------------------$MultiPartBoundary" + $HeaderParameters['Content-Type'] = "$ContentType; boundary=$MultiPartBoundary" + } } # add default headers if any @@ -72,7 +78,6 @@ function Invoke-PSApiClient { $HeaderParameters[$header.Name] = $header.Value } - # construct URL query string $HttpValues = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) foreach ($Parameter in $QueryParameters.GetEnumerator()) { @@ -90,9 +95,34 @@ function Invoke-PSApiClient { # include form parameters in the request body if ($FormParameters -and $FormParameters.Count -gt 0) { - $RequestBody = $FormParameters + if (![string]::IsNullOrEmpty($MultiPartBoundary)) { + $RequestBody = "" + $LF = "`r`n" + $FormParameters.Keys | ForEach-Object { + $value = $FormParameters[$_] + $isFile = $value.GetType().FullName -eq "System.IO.FileInfo" + + $RequestBody += "--$MultiPartBoundary$LF" + $RequestBody += "Content-Disposition: form-data; name=`"$_`"" + if ($isFile) { + $fileName = $value.Name + $RequestBody += "; filename=`"$fileName`"$LF" + $RequestBody += "Content-Type: application/octet-stream$LF$LF" + $RequestBody += Get-Content -Path $value.FullName + } else { + $RequestBody += "$LF$LF" + $RequestBody += ([string]$value) + } + $RequestBody += "$LF--$MultiPartBoundary" + } + $RequestBody += "--" + } else { + $RequestBody = $FormParameters + } } + + if ($Body -or $IsBodyNullable) { $RequestBody = $Body if ([string]::IsNullOrEmpty($RequestBody) -and $IsBodyNullable -eq $true) {