Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

restapi - PowerShell Rst API upload text file is fail

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...