Upload file using command line

Is there an API or a command line utility that will allow me to upload directly to astroprint on a raspberry pi?

Currently it takes at least 7 mouse clicks to upload a .gcode file to astrobox and start a print.

Idealy I would do something with curl or some other command line utility

curl --upload-file foo.gcode astrobox.local/uploads

I have tried using scp to copy the files directly to the rasbperry pi /Astrobox/Uploads directory. The files uploaded by scp are never detected in the gui.

You’re on the right track. The URL to upload would be

POST /api/files/local

You can add print=1 to the POST paramaters if you’d like the file to start to print.

Also all requests to the API need a header: X-Api-Key, the API Key can be found in the AstroBox Web UI under advanced software settings.

Thanks for the suggestion. I find that I get error ‘No file included’ regardless of how I upload it

curl -X POST --header 'X-Api-Key:xxxxxxxx' -H 'Content-Type: application/json'  --data '@Grid_Fin5.gcode'  http://astrobox1.local/api/files/local
No file included

curl -X POST --header 'X-Api-Key: xxxxxxx' --data-binary "@Grid_Fin5.gcode"  http://astrobox1.local/api/files/local
No file included

** Update **

looking at the source, it appears that you must include the file name in the url

curl -X POST --header 'X-Api-Key: xxxxxxx' --data-binary "@Grid_Fin5.gcode"  http://astrobox1.local/api/files/local/Grid_Fin5.gcode

however this still results in a different error

File not found on ‘local’: Grid_Fin_PLA_ULTRADETAIL1.gcode

Which originates here:

Unfortunately I don’t understand what the _verifyFileExists function is checking, or what it is erroring on

I suspect there may be a bug in the api POST function, since it always returns File not found on local

Looking at the logs, the file uploads successfully,

< HTTP/1.1 100 (Continue)
* We are completely uploaded and fine

but then returns a 404.

< HTTP/1.1 404 NOT FOUND

Full output

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" --data-binary "@${ASTRO_FOLDER_PATH}/${ASTRO_FILE_NAME}"  "${ASTRO_SERVER}/api/files/local/${ASTRO_FILE_NAME}" --verbose
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 192.168.1.46...
* TCP_NODELAY set
* Connected to astrobox1.local (192.168.1.46) port 80 (#0)
> POST /api/files/local/Falcon_9_Second_Stage_1_PLA_OPTIMAL.gcode HTTP/1.1
> Host: astrobox1.local
> User-Agent: curl/7.54.0
> Accept: */*
> X-Api-Key: xxxxxxxxxxxx
> Content-Length: 24275440
> Content-Type: application/x-www-form-urlencoded
> Expect: 100-continue
>
< HTTP/1.1 100 (Continue)
* We are completely uploaded and fine
< HTTP/1.1 404 NOT FOUND
< Content-Length: 68
< Content-Type: text/html; charset=utf-8
< Server: TornadoServer/4.4.2
<
* Connection #0 to host astrobox1.local left intact
File not found on 'local': Falcon_9_Second_Stage_1_PLA_OPTIMAL.gcode

I hope to have some time next week to try this. In the mean time, have you tried doing a multipart/form-data upload?

Yes, I’ve also tried multipart form data uploads. I’m sure I’m missing something simple.

Here are some more examples of what I’m trying.

I’m currently searching the source for a javascript or python example that I can leverage.

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" -F  "upload=foo.txt"  "${ASTRO_SERVER}/api/files/local/foo.txt" --verbose

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" -F  "local=foo.txt" "${ASTRO_SERVER}/api/files/local/foo.txt" --verbose

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" -F  "local=foo.txt" -H 'Content-Type: multipart/form-data'  "${ASTRO_SERVER}/api/files/local/foo.txt" --verbose

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" -F  "local=@./foo.txt" -H 'Content-Type: multipart/form-data'  "${ASTRO_SERVER}/api/files/local/foo.txt" --verbose

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" -F  "target=foo.txt" -H 'Content-Type: multipart/form-data'  "${ASTRO_SERVER}/api/files/local/foo.txt" --verbose

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" --form "target=@foo.txt"  "${ASTRO_SERVER}/api/files/local/foo.txt" --verbose

curl -X POST --header "X-Api-Key: ${ASTRO_API_KEY}" --form "filename=@foo.txt"  "${ASTRO_SERVER}/api/files/local/foo.txt" --verbose

I’m still struggling to upload a file.

I’ve found an ajax request that I’m using as an example. It appears to be using content type of json

Converting that to cURL does not work

curl -X POST \
--header "X-Api-Key: ${ASTRO_API_KEY}" \
--header "Content-Type: application/json; charset=UTF-8" \
-F "data=@${ASTRO_FOLDER_PATH}/${ASTRO_FILE_NAME}" \
"${ASTRO_SERVER}/api/files/local/${ASTRO_FILE_NAME}" 

I’ve also tried uploading using postman. It defaults to using multipart form-data. It gives the same error

File not found on ‘local’: Payload_Faring_2_2.gcode

curl -X POST \
  http://astrobox1.local/api/files/local/Payload_Faring_2_2.gcode \
  -H 'Cache-Control: no-cache' \
  -H 'Postman-Token: xxxxxxx' \
  -H 'X-Api-Key: xxxxxxxxx' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundaryxxxxxxx' \
  -F =@/Users/sowen/Desktop/3dprints/falcon9/Payload_Fairing_2_2.gcode

I’ve also tried as a raw binary

curl -X POST \
  http://astrobox1.local/api/files/local/Payload_Faring_2_2.gcode \
  -H 'Cache-Control: no-cache' \
  -H 'Postman-Token: xxxxx' \
  -H 'X-Api-Key: xxxxx'

I’m quite confused, I’m not sure what astrobox is looking for.

I’m still unable to get this to work properly. Is this a bug in the api?

Did you ever figure this out? I second the desire.

No, I never figured it out.

Oh well. Stay strong.

Figured it out

curl -X POST \
  http://astrobox1.local/api/files/local \
  -H 'Postman-Token: xxxxxxx' \
  -H 'X-Api-Key: xxxxxxx' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F file=@/Users/spencerowen/Desktop/3dprints/foobar.gcode

The above command was generated by postman, so it has some things that you really don’t need. A simpler command might look like:

curl -X POST \
  http://astrobox1.local/api/files/local \
  -H 'X-Api-Key: xxxxxxx' \
  -H 'content-type: multipart/form-data;'  \
  -F file=@/Users/spencerowen/Desktop/3dprints/foobar.gcode

If you want to upload and print in 1 step, add -F print=true

2 Likes

Very cool spencer! When we have a little more time we will document the API.

Postman FTW! Well done @Spencer_Owen.
I have an idea for this… I’ll let you know if it goes well.

Aha.
I’m on a Mac in the moment, and Alfred is my best friend.

I created a simple Alfred workflow that takes a file (from a search/ highlighted in finder…), sends it to astroprint and starts printing. Bliss.

Demo:

Download the workflow

Note, you have to put your API key and Astroprint.local URL in the environment variables!

2 Likes

That is pretty sweet. Which print setting does it use? Do you have an option to change it to different presets?

This is for uploading gcode, not stl. Meaning you are uploading pre-sliced files with all the print settings already applied. (This is for peeps using desktop slicers.)

It could be modified for stl I’m sure, but someone would need to dig into the POST format for that…

@Filip, We don’t allow upload of STL at the moment because the box won’t know what to do with it. You would need to create an AstroPrint App and use the cloud APIs

1 Like