I have trouble to upload text file using PowerShell Invoke-RestMethod. Getting info from the API is working fine, but upload is fail:
This is working:
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$username = "xxxxxx"
$password = ConvertTo-SecureString "xxxxxx" -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
$Body = @{
'filename' = 'r1.txt';
'content_type' ='text/plain';
'file'=[IO.File]::ReadAllBytes('c:usersals
1.txt');
}
$uri = "https://x.x.x.x/api/v1/"
Invoke-RestMethod -Uri $uri -Method Get -Credential $Cred
But when running with the following command it is failing: Invoke-RestMethod -Uri $uri -Method Post -Body $Body -Credential $Cred -or- Invoke-RestMethod -Uri $uri -Method Put -Body $Body -Credential $Cred
Getting this error:
Invoke-RestMethod : The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF At line:22 char:1
- Invoke-RestMethod -Uri $uri -Method Post -Credential $Cred
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod],
WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I will appreciate any assist with this issue.
Thanks Tal
question from:https://stackoverflow.com/questions/65881487/powershell-rst-api-upload-text-file-is-fail