简体   繁体   中英

Powershell equivalent of CURL command

The CURL command is given below.

curl -ik -X PUT -H "X-API-Version: 600" -H "Auth:$AUTH" -H "Content-Type: multipart/form-data" -F "filename=@./temp.crl;type=application/pkix-crl" https://$HOST_IP/rest/$ALIASNAME/crl

I wanted to get the Powershell equivalent using Invoke-RestMethod.I tried the below code.

$url='https://'+$hostname+'/rest/+$aliasname+'/crl'
$url
$hdrs=@{}
$hdrs.Add('X-API-Version', '600');
$hdrs.Add('Auth', $Auth);
$hdrs.Add('Content-Type', 'application/json');
$path=get-location
$FileContent = [IO.File]::ReadAllBytes(temp.crl)
$file=@{'filename'=$FileContent}
Invoke-RestMethod -Uri $url -ContentType 'mutlipart/form-data' -Method Put -Headers $hdrs -Body $file

It gives back 400 (Bad Request error)

Use the -InFile Parameter, Try this:

$FilePath = '/temp.crl' # Make sure the path is correct #
$Headers = @{"X-API-Version" = 600;Auth = $AUTH}
Invoke-RestMethod -Uri "https://$HOST_IP/rest/$ALIASNAME/crl" `
-ContentType 'multipart/form-data' `
-Headers $Headers -Method Put -InFile $FilePath

This code snippet should resolve your problem. Your question had multiple typos and conflicting logic.

$restArgs = @{
    Uri         = "https://$hostname/rest/$aliasname/crl"
    Headers     = @{
        'X-API-Version' = 600
        Auth            = $Auth
    }
    Method      = 'Put'
    Body        = @{
        filename = [IO.File]::ReadAllBytes("$PSScriptRoot\temp.crl")
    }
    ContentType = 'multipart/form-data'
}
Invoke-RestMethod @restArgs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM