Hi all !
I’m trying to do a basic call to a REST API to upload a file.
I have a perfectyl working example with POSTMAN ( credentials, form-data, document as File in the body ).
If I redirect the POST to a local debug server, here is what I receive :
POST / HTTP/1.1
Authorization: Basic bWNpdGhhcmVsOk5hdHVyZTE1NA==
User-Agent: PostmanRuntime/7.24.0
Accept: /
Cache-Control: no-cache
Postman-Token: 80e88c17-ae1d-454b-bc9a-71794caae348
Host: localhost:8989
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------864163748135484180452120
Content-Length: 409483
If I launch the generated Powershell code, I get an error from the API telling me the file is empty.
If I redirect the POST to a local debug server, here is what I receive :
POST / HTTP/1.1
Authorization: Basic bWNpdGhhcmVsOk5hdHVyZTE1NA==
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 10.0; fr-FR) WindowsPowerShell/5.1.18362.628
Content-Type: application/x-www-form-urlencoded
Host: localhost:8989
Content-Length: 0
Connection: Keep-Alive
I notice at first sight that the content-type is application/x-www-form-urlencoded instead of multipart/form-data .
Here is the sample code generated :
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization", "Basic bWNpdGhhcmVsOk5hdHVyZTE1NA==") $multipartContent = [System.Net.Http.MultipartFormDataContent]::new() $multipartFile = '/L:/TBD_CGVWorkplaceTeamWay.pdf' $FileStream = [System.IO.FileStream]::new($multipartFile, [System.IO.FileMode]::Open) $fileHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data") $fileHeader.Name = "file" $fileHeader.FileName = "/L:/TBD_CGVWorkplaceTeamWay.pdf" $fileContent = [System.Net.Http.StreamContent]::new($FileStream) $fileContent.Headers.ContentDisposition = $fileHeader $multipartContent.Add($fileContent) $body = $multipartContent $response = Invoke-RestMethod 'http://localhost:8989' -Method 'POST' -Headers $headers -Body $body $response | ConvertTo-Json
Any idea to help me fix that code ?