我确实知道这个问题已经被问过很多次了,但是我遇到了一个错误,这是我在论坛上没有看到的,我希望有人可能知道我在做什么错。
那我在做什么?我正在使用Expensify API来自动配置新员工。我已经有一个2,000行的新聘用脚本,并且我只是想向其中添加更多SaaS应用程序,所以我需要做的...更少。
cURL请求应该看起来像这样:
curl -X POST 'https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations' \
-H 'Expect:' \
-F 'requestJobDescription={
"type": "update",
"credentials": {
"partnerUserID": "_REPLACE_",
"partnerUserSecret": "_REPLACE_"
},
"inputSettings": {
"type": "employees",
"policyID":"0123456789ABCDEF",
"fileType": "csv"
}
}' \
-F 'data=@employeeData.csv'
我的脚本如下:
$expensify_csv = @(
[pscustomobject]@{
EmployeeEmail = $email
ManagerEmail = $mngr_email
Admin = $expensify_admin
ForwardManagerEmail = $fwd_email
}
) | Export-Csv -Path C:\expensify.csv -NoTypeInformation
$expensify_csv = [IO.File]::ReadAllText('C:\expensify.csv');
$json = [ordered]@{
"requestJobDescription" = @{
"type" = "update";
"credentials" = @{
"partnerUserID" = $expensify_id;
"partnerUserSecret" = $expensify_secret;
}
"inputSettings" = @{
"type" = "employees";
"policyID" = "F9CC59BCD4521BB2";
"fileType" = "csv";
}
};
"data" = $expensify_csv
} | ConvertTo-Json -Depth 10
Write-Host $json
$check = Invoke-RestMethod `
-Method Post `
-Uri $expensify_url `
-Body $json `
-ContentType multipart/form-data `
Write-Host $check
exit
以及正在返回的错误:
Invoke-RestMethod :
Error
body{
font-family: Arial;
}
pre{
padding: 5px;
background-color: #ddd;
border-top: 2px solid #999;
}
An error occurred
Internal Server Error
错误似乎与CSS有关?我也不知道很奇怪。我已经和这个API作战了一段时间了,感谢您的反馈!
答案 0 :(得分:0)
已更新
现在我明白了问题所在,curl拥有-F
在body
内添加一个请求,而curl在后台进行了一些其他编辑,例如添加了boundary
。这不是Invoke-RestMethod
的本机,因此我们需要编写它。
$FilePath = 'C:\employeeData.csv';
$URL = 'https://integrations.expensify.com/Integration-Server/ExpensifyIntegrations';
$importedCSV = Get-Content $filepath -Raw
$boundary = [System.Guid]::NewGuid().ToString();
$LF = "`r`n";
$bodyhash = [ordered]@{
"type" = "update";
"credentials" = @{
"partnerUserID" = "_REPLACE_";
"partnerUserSecret" = "_REPLACE_";
}
"inputSettings" = @{
"type" = "employees";
"policyID" = "F9CC59BCD4521BB2";
"fileType" = "csv";
}
}
$bodyJSON = $bodyhash | ConvertTo-Json
$nestedBody = (
"--$boundary",
"Content-Disposition: form-data; name=`"requestJobDescription`"",
"Content-Type: application/json$LF",
"$($bodyJSON)",
"--$boundary",
"Content-Disposition: form-data; name=`"data`"; filename=`"employeeData.csv`"",
"Content-Type: application/octet-stream$LF",
$importedCSV,
"--$boundary--$LF"
) -join $LF
$sendRequest=@{
Uri = $URL
Method = "Post"
ContentType = "multipart/form-data; boundary=`"$boundary`""
Body = $nestedBody
}
Invoke-RestMethod @sendRequest
此示例为我提供了Authentication Error
,与之前的internal server error
不同。
2个答案(不是公认的答案)将我引向该解决方案-powershell invoke-restmethod multipart/form-data
P.S。解决此问题,导致我自己解决关于如何使用powershell nested HTTP request
的问题。