[英]Invoke-RestMethod to Send a CSV
I do understand that this question has been asked and answered many times, but I'm getting an error that I've not seen on the forums and I'm hoping someone might know what I'm doing wrong.我知道这个问题已经被问过很多次了,但是我遇到了一个我在论坛上没有看到的错误,我希望有人可能知道我做错了什么。
So, what am I doing?那么,我在做什么呢? I am using the Expensify API to automate the provisioning of new hires.
我正在使用 Expensify API 来自动配置新员工。 I already have a 2,000 line new hire script and I'm just trying to add more SaaS apps to it so I have to do... less.
我已经有一个 2,000 行的新雇用脚本,我只是想向其中添加更多的 SaaS 应用程序,所以我必须做……更少。
The cURL request is supposed to look like this: 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'
And my script looks like this:我的脚本如下所示:
$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
And the error that is being returned:以及正在返回的错误:
Invoke-RestMethod :
Error
body{
font-family: Arial;
}
pre{
padding: 5px;
background-color: #ddd;
border-top: 2px solid #999;
}
An error occurred
Internal Server Error
It looks like the error has something to do with CSS?看起来该错误与 CSS 有关? I have no idea though.
不过我不知道。 Very strange.
很奇怪。 I've been battling this API for some time now and I'd appreciate any feedback!
我一直在与这个 API 作斗争一段时间,如果有任何反馈,我将不胜感激!
UPDATED更新
Now I see what the problem is, curl has that -F
to add one more request inside the body
and curl does some additional edits in the background, like adding a boundary
.现在我明白了问题所在, curl 有
-F
在body
中添加一个请求,而 curl 在后台进行了一些额外的编辑,例如添加boundary
。 This is not native to Invoke-RestMethod
so we need to write it.这不是
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
This example, gives me Authentication Error
as I would expect, unlike before with the internal server error
.这个例子,正如我所期望的那样给了我
Authentication Error
,这与之前的internal server error
不同。
2 of the answers (not the accepted one) from the following post lead me to that solution - powershell invoke-restmethod multipart/form-data以下帖子中的 2 个答案(不是被接受的答案)引导我找到该解决方案 - powershell invoke-restmethod multipart/form-data
PS Working on this issue, lead to the resolution on my own issue on how to do nested HTTP request
with powershell. PS 解决这个问题,导致解决我自己的问题,即如何使用 powershell 执行
nested HTTP request
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.