Invoke-RestMethod 发送 CSV
Invoke-RestMethod to Send a CSV
我知道这个问题已被多次询问和回答,但我遇到了一个我在论坛上从未见过的错误,我希望有人知道我做错了什么。
所以,我在做什么?我正在使用 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 斗争了一段时间,非常感谢任何反馈!
已更新
现在我明白问题出在哪里了,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
。
来自以下 post 的 2 个答案(未被接受)引导我找到该解决方案 -
P.S。解决这个问题,导致解决我自己的问题,即如何使用 powershell nested HTTP request
。
我知道这个问题已被多次询问和回答,但我遇到了一个我在论坛上从未见过的错误,我希望有人知道我做错了什么。
所以,我在做什么?我正在使用 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 斗争了一段时间,非常感谢任何反馈!
已更新
现在我明白问题出在哪里了,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
。
来自以下 post 的 2 个答案(未被接受)引导我找到该解决方案 -
P.S。解决这个问题,导致解决我自己的问题,即如何使用 powershell nested HTTP request
。