我正在尝试在Powershell中创建等效于
的命令
curl -u username:abcd -i -F name=files -F filedata=@employees.csv https://myservice.com/v1/employees/csv
我需要在请求中输入文件名。因此在Powershell中
$FilePath = 'employees.csv'
$FieldName = 'employees.csv'
$ContentType = 'text/csv'
$username = "user"
$password = "..."
$FileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
$FileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new('form-data')
$FileHeader.Name = $FieldName
$FileHeader.FileName = Split-Path -leaf $FilePath
$FileContent = [System.Net.Http.StreamContent]::new($FileStream)
$FileContent.Headers.ContentDisposition = $FileHeader
$FileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse($ContentType)
$MultipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$MultipartContent.Add($FileContent)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($username):$($password)" ))
$Response = Invoke-WebRequest -Headers @{Authorization = "Basic $base64AuthInfo" } -Body $MultipartContent -Method 'POST' -Uri 'https://myservice.com/v1/employees/csv'
是否有更好(更短)的方法,所以我在Content Disposition中有一个文件名?
$body = get-content employees.csv -raw
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("user:pass" ))
Invoke-RestMethod -Headers @{Authorization = "Basic $base64AuthInfo" } -uri url -Method Post -body $body -ContentType 'text/csv
# a flag -ContentDispositionFileName would be great
答案 0 :(得分:0)
猜测端点将接受什么,但这是您在curl
中的powershell
请求的示例:
$u, $p = 'username', 'password'
$b64 = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${u}:$p"))
$invokerestmethodParams = @{
'Uri' = 'https://myservice.com/v1/employees/csv'
'Method' = 'POST'
'Headers' = @{ Authorization = "Basic $b64" }
'InFile' = 'C:\path\to\employees.csv'
'SessionVariable' = 's' # use $s to view content headers, etc.
}
$output = Invoke-RestMethod @invokerestmethodParams