使用powershell将文档上载到Salesforce

时间:2018-03-12 08:11:14

标签: powershell curl salesforce

我正在尝试使用PowerShell在Salesforce应用程序中上传文本文件。

我试图在PowerShell中模仿的卷曲代码是:

curl -X POST -H "Authorization: {SESSION_ID}" \
-H "Content-Type: multipart/form-data" \
-F file=@document.txt \
-F "name__v=myDocument" \
-F "type__v=Undefined" \
-F "lifecycle__v=Unclassified" \
https://{server}/api/{version}/objects/documents

以下是我从网上提示的PowerShell代码:

$URL="https://{server}/api/{version}/auth"
$CT="application/x-www-form-urlencoded"
$User="user"
$Password="pass"
$SessionID=(Invoke-RestMethod -Method Post -Uri $URL -Body @{"Content-Type" = $CT; "username" = $User; "password" = $Password}).sessionId

$URL="https://{server}/api/{version}/objects/documents"
$boundary=[System.Guid]::NewGuid().ToString()
$LF="`r`n"
$File=Get-Content("....\test.txt")

$fileBin=[System.IO.File]::ReadAllBytes("....\test.txt")
$enc=[System.Text.Encoding]::GetEncoding("iso-8859-1")
$fileEnc=$enc.GetString($fileBin)

$Body1=(
    "--$boundary",
    "Content-Disposition: form-data; name__v=`"kaz.txt`"; type__v=`"Undefined`"; lifecycle__v=`"Unclassified`"",
    "Content-Type: application/octet-stream$LF",
    $fileEnc,
    "--$boundary--$LF"
) -join $LF

$Body2 = (
    "file=$File; name__v=`"kaz.txt`"; type__v=`"Undefined`"; lifecycle__v=`"Unclassified`""
)
Invoke-RestMethod -Method POST -Uri $URL -Headers @{"Authorization" = $SessionID; "Content-Type" = "multipart/form-data; boundary=`"$boundary`""} -Body $Body1
Invoke-RestMethod -Method POST -Uri $URL -Headers @{"Authorization" = $SessionID; "Content-Type" = "multipart/form-data; boundary=`"$boundary`""} -Body $Body2

这些都不起作用,我收到错误

PARAMETER_REQUIRED lifecycle__v:缺少必需参数:lifecycle__v。

1 个答案:

答案 0 :(得分:0)

我修改了身体,现在工作正常

$Body = (
   "--$boundary", "Content-Disposition: form-data; name=`"file`"; filename=`"test.docx`"", "Content-Type: application/octet-stream$LF", $fileEnc,
   "--$boundary", "Content-Disposition: form-data; name=`"lifecycle__v`"", "", "Unclassified",
   "--$boundary", "Content-Disposition: form-data; name=`"type__v`"", "", "Undefined",
   "--$boundary--"
)-join $LF

Invoke-RestMethod -Method POST -Uri $URL -Headers @{"Authorization" = $SessionID; "Content-Type" = "multipart/form-data; boundary=$boundary"} -Body $Body