I’m trying to turn a standard curl request into something that can be run in a powershell script.
Here’s the curl that works consistently:
curl -X POST -F "file=@C:yourfilelocationExample.csv" -H "Authorization: Bearer [INSERT BEARER TOKEN]" https://example-api-or-endpoint.com
Here’s the powershell that consistently fails:
$uri = "https://example-api-or-endpoint.com" $outputCsvPath = "C:yourfilelocationExample.csv" $headers = @{"Authorization" = "Bearer [BEARERTOKEN]"} Invoke-WebRequest -Uri $uri -Method Post -InFile $outputCsvPath -ContentType "multipart/form-data" -Headers $headers
Expected Result:
{"file_id":"FILEID","message":"File sent successfully","status":"success"}
Actual Result:
{"message":"No file part in the request","status":"error"}
I’ve tried using RestMethod, changing InFile to Body, made sure the file path is correct and the file exists, and I keep getting an error saying that there isn’t a file part in the request.
It’s worth mentioning that the URL I’m using is a Google Cloud Function endpoint, so not quite an API but the CURL statement I mentioned in the details works 100% of the time but the powershell version of it fails 100% of the time.
For some additional context, the POST request should be sending the entire file, not it’s contents.
Seph Rodriguez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.