File upload to OctoPrint from PowerShell failing

What is the problem?

I'm using Invoke-Restmethod to upload a file to Octoprint, however I'm getting an error back of "No file to upload and no folder to create". I've taken a network trace and viewing the POST data with the file upload documentation (File operations β€” OctoPrint master documentation) and I'm not seeing anything off. The gcode is definitely in the body between the boundary and I see the filename being posted as well.

POST /api/files/local HTTP/1.1

Host: octodev


User-Agent: Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.19041; en-US) PowerShell/7.2.0

Content-Length: 6575664

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryDeC2E3iWbTv1PwMC


Content-Disposition: form-data; name="file"; filename="roo2.gcode"

Content-Type: application/octet-stream



;Filament used: 4.61944m

;Layer height: 0.1

the remainder of the gcode here and then:

;End of Gcode


What did you already try to solve it?

I tried bumping up the file API logging but it doesn't seem to give me anymore information (doesn't seem like it notices an attempt at all). API key works, I can interact with other REST endpoints without issue.

Can you share your PowerShell script please?

You need a filepath option as part of the form data, possibly instead of where you have filename="roo2.gcode", does switching to filepath="roo2.gcode" work?

Recently worked with someone on discord with a similar issue, trying to use JavaScript to upload a file manually - this was the result

This might be wrong - I'm just looking at the code & docs, maybe it was something specific to the library being used at the time, not the actual http request.

I thought maybe the extra dashes before the last boundary was causing it to not interpret the file contents, however if I remove those Octoprint becomes unstable. I can't reach the web interface and SSH is iffy. I noticed in a network trace that the Pi sends tcp window resizes a the way down to zero and net traffic slows down.

This works fine over here:

$uri = "REDACTED"
$apikey = "REDACTED"
$file = "REDACTED"

$headers = @{
    "X-Api-Key" = $apikey
$boundary = [System.Guid]::NewGuid().ToString();
$LF = "`r`n"

$fileName = [System.IO.Path]::GetFileName($file)
$fileContent = [System.IO.File]::ReadAllText($file)
$bodyLines = (
    "Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
    "Content-Type: application/octet-stream",
) -join $LF

Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -body $bodyLines -contenttype "multipart/form-data; boundary=$boundary"

That worked. I'll look this over to see what was done wrong originally. Thank you so much.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.