Reputation: 33
I have a Jenkins Job that requires a file as parameter (I'm using the file parameter plugin). When I made the call to Jenkins I notice that the file parameter doesn't get sent but the other parameters do (CHOICE and string), what I'm doing wrong?
$credPair = "user:token";
$encodedCredentials = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credPair));
$headers = @{
"Authorization" = "Basic $encodedCredentials";
"Content-Type" = "multipart/form-data";
}
$choiceParam = "option"
$stringParam = "value"
$file = "file_path"
$Body = @{ "File" = [IO.File]::ReadAllText($file); }
$url = "https://server/job/buildWithParameters?CHOICE=" + $choiceParam + '&string' + $stringParam
try {
Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $Body
}
catch {
Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
write-host "Error details: " $_.ErrorDetails.Message
exit
}
I already tried the next questions and didn't work multipart/form-data file upload with PowerShell powershell invoke-restmethod multipart/form-data How to send multipart/form-data with PowerShell Invoke-RestMethod Using PowerShell Invoke-RestMethod to POST large binary multipart/form-data
I want to send a file parameter to Jenkins via HTTP Post Request with PowerShell
Upvotes: 2
Views: 541
Reputation: 27806
Note: As it turns out, the solution below only works for freestyle jobs. If you need a solution for pipeline jobs, you may use this solution (which requires the file parameter plugin).
I have used Invoke-RestMethod
with parameter -Form
to upload files to Jenkins successfully.
Something like that should work:
$credPair = "user:token";
$encodedCredentials = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credPair));
$headers = @{
"Authorization" = "Basic $encodedCredentials";
# NOTE: no Content-Type header, Invoke-RestMethod adds it automatically when using -Form
}
$choiceParam = "option"
$stringParam = "value"
$file = "file_path"
$form = @{
json = ConvertTo-Json -Compress @{
parameter = @(
@{ name = "INSERT_NAME_OF_CHOICE_PARAM"; value = $choiceParam }
@{ name = "INSER_NAME_OF_STRING_PARAM"; value = $stringParam }
# IMPORTANT: value of file must be equal to file key used to assign FileInfo below
@{ name = "INSERT_NAME_OF_FILE_PARAM"; file = "file1" }
)
}
# Don't read the file on your own, Invoke-RestMethod will do it
file1 = [IO.FileInfo] $file
}
# NOTE: /build instead of /buildWithParameters
$url = "https://server/job/INSERT_JOBNAME/build"
try {
Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Form $form
}
catch {
Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
write-host "Error details: " $_.ErrorDetails.Message
exit
}
For reference, I got the basic principle for uploading files to a Jenkins job from this answer.
Upvotes: 0
Reputation: 33
@zett42 I solve it, apparently only in the form you put the file, the others params are put in the url
$credPair = "user:token";
$encodedCredentials = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($credPair));
$headers = @{
"Authorization" = "Basic $encodedCredentials";
}
$choiceParam = "option"
$stringParam = "value"
$file = "file_path"
$form = @{
INSERT_NAME_OF_FILE_PARAM = Get-Item $file
}
$url = "https://server/job/INSERT_JOBNAME/buildWithParameters?INSERT_NAME_OF_CHOICE_PARAM=" + $choiceParam + '&INSERT_NAME_OF_STRING_PARAM=' + $stringParam
try {
Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Form $form
}
catch {
Write-Host "StatusCode: " $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription: " $_.Exception.Response.StatusDescription
write-host "Error details: " $_.ErrorDetails.Message
exit
}
Upvotes: 1