Upload file using command line


#1

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.


#2

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.


#3

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


#4

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

#5

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


#6

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

#7

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.


#8

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