简体   繁体   中英

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. 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.

The cURL request is supposed to look like this:

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? I have no idea though. Very strange. I've been battling this API for some time now and I'd appreciate any feedback!

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 . This is not native to Invoke-RestMethod so we need to write it.

$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 .

2 of the answers (not the accepted one) from the following post lead me to that solution - 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM