簡體   English   中英

調用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 有-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

PS 解決這個問題,導致解決我自己的問題,即如何使用 powershell 執行nested HTTP request

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM