aritertsunen
aritertsunen

Reputation: 11

File upload via Pipedrive API

I need to include the lead_id in the body with the file content. The file uploads well when I use

$data = Invoke-RestMethod -Uri $URL -Method 'Post' -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $BodyLines. 

Naturally the file is not attached to the lead. When I use

$data = Invoke-RestMethod -Uri $URL -Method 'Post' -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $Body

I always get error 400. Specification of $Body is wrong, but how?

#URL for Deal listing with your $company_domain and $api_token variables
$URL = 'https://' + $company_domain + '.pipedrive.com/api/v1/files?api_token=' + $api_token
$URL
#
$FilePath = $LOCALpath + "\webtilaus\Tarjouspyynto_2023-02-03_09-50-44.txt" 
$data =@()

$fileBytes = [System.IO.File]::ReadAllBytes($FilePath);
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString(); 
$LF = "`r`n";
""
$BodyLines = (
"--$boundary",
    "Content-Disposition: form-data; name=`"file`"; filename=`"temp.txt`"",
    "Content-Type: application/octet-stream$LF",
    $fileEnc,
    "--$boundary--"+ $LF 
) -join $LF

$Body = @{
    "deal_id" = "25"
    "file" = $bodyLines
    }

$data = Invoke-RestMethod -Uri $URL -Method 'Post' -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $BodyLines    # This uploads
$data = Invoke-RestMethod -Uri $URL -Method 'Post' -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $Body         # This gives error 400`

Upvotes: 1

Views: 65

Answers (0)

Related Questions