【问题标题】:Invoke-RestMethod to Send a CSVInvoke-RestMethod 发送 CSV
【发布时间】:2019-10-25 19:03:16
【问题描述】:

我明白这个问题已被多次询问和回答,但我遇到了一个我在论坛上没有看到的错误,我希望有人可能知道我做错了什么。

那么,我在做什么?我正在使用 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 斗争了一段时间,如果有任何反馈,我将不胜感激!

【问题讨论】:

  • 您正在尝试发送 json... 但您发送的内容类型不匹配。

标签: powershell rest api powershell-5.0


【解决方案1】:

更新

现在我知道问题出在哪里了,curl 让 -Fbody 中再添加一个请求,并且 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

附:解决这个问题,解决我自己关于如何使用 powershell 处理 nested HTTP request 的问题。

【讨论】:

  • 所以我进行了这些更改,但我仍然看到相同的错误...我的 $body 变量如下所示:System.Collections.DictionaryEntry System.Collections.DictionaryEntry
  • @DerekRitchison 我已经更新了我的答案,尝试新的解决方案并告诉我。
  • 天哪。我刚收到 Expensify 的 200 条回复。伙计,这太神奇了,我不确定我是否会自己解决这个问题。我正要放弃 PowerShell,看看我是否可以在 Ruby 中更轻松地做到这一点。你真棒。如果可以的话,我会投票 100 次。
  • 很高兴听到这个消息,如果您认为这已经足够了,您可以将我的回复标记为答案并点赞以帮助他人和我自己。
  • 我确实投了赞成票,但不幸的是我的声誉还不够高。我需要为此努力。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多