Jump to content
Ultimaker Community of 3D Printing Experts

POST print_job with nodeJS


Recommended Posts

Posted · POST print_job with nodeJS

Hello,

I try to start a print job over the API of my Ultimaker 3. I read the example in this post

 and tried to port the code from python to nodeJS, but the POST request is just hanging.

 

Code:

var request = require("request");
var fs = require('fs');
  
var formData = {
    "file": {
      value: fs.createReadStream('C:\\Users\\efentzahn\\IKIMUNI_Schornstein_simple2.gcode'),
      options: {
        filename: 'IKIMUNI_Schornstein_simple2.gcode'
      }
    }
};

request.post( {url: 'http://10.53.1.114/api/v1/print_job',
              auth: { user: '45fa38b52ea5a998c085145930356bd6', pass: '63bb0fc18a39ba7c787805e80f0be1211144f4083de634deb57f7e7819a640ec', sendImmediately: false},
              formData: formData},
function(error, response, body){
    if (!error && response.statusCode == 201){
        console.log("Print started.");
    }
    else{
        console.log('Code : ' + response.statusCode)
        console.log('error : ' + error)
        console.log('body : ' + body);
    }
});

 

The auth is working. It must be a problem with the formData. Have anyone tried this with nodeJS until now?

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS

    It's been a while since I developed anything in Node, but in general the code looks ok.

     

    How long have you waited? Maybe you're trying to post a very big file?

     

    As a first thing, I would try doing an /api/v1/auth/verify request. Just as a sanity check to test if you are correctly submitting the username and password.

     

    If that is working, maybe you can try some of the easier calls that don't need a multipart form, like setting the LED brightness?

     

    Otherwise, you may need to check your multipart form code. I know this can be complicated in many languages and it looks like the syntax is ok, but you never know.

    (I used Google to find this: https://github.com/request/request#multipartform-data-multipart-form-uploads )

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS

    The login is working and doing PUT request like pause a print works fine in node.js.

    The file is 340kbyte "big". That should be transmitted very fast. After a few seconds (maybe 10?) node ends with the error: "socket hang up".

     

    I think the problem is the multipart form part, but i have no clue what it is exactly. I already googled a lot and the code posted above is developed with this example from the request library. But the correct names and needed fields in the multipart form is dependent to the Ultimaker API. So, I have the strong feeling that a Ultimaker developer have to take a look at the code.

  • Link to post
    Share on other sites
    • 11 months later...
    Posted · POST print_job with nodeJS

    Hi everybody ! Any update on this one? i'm facing the same result on nodejs.

     

    Moreover : 

    I tried with multiple tools : 

    - Insomnia : works as expected -> It's possible to start a print job

    - Curl command line : works as expected -> It's possible to start a print job

    - Post Man : Cannot make it work ( error code : 400 Bad request :  "message": "No file received")

    - Python3 : same as PostMan ( the file seems to be received by the printer as i see message in printer's log : (but seems empty?)

     

    Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[450]: 2019-02-12 10:58:10,096 INFO     printJob        Saving received file UM3_MusicNoteKeyChainXL.gcode",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[450]: 2019-02-12 10:58:10,118 INFO     printJob        File saved as /tmp/UM3_MusicNoteKeyChainXL.gcode",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: 2019-02-12 10:58:10,125 INFO     controller      Request to start procedure: PRINT",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: 2019-02-12 10:58:10,130 ERROR    controller      Exception in procedure start",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: Traceback (most recent call last):",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: File \"/usr/share/griffin/griffin/printer/controller.py\", line 455, in startProcedure",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: if not procedure.setup(parameters):",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: File \"/usr/share/griffin/griffin/printer/procedures/print/printProcedure.py\", line 102, in setup",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: self._parse_header_step.getFileHandler().cleanup()",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[418]: AttributeError: 'NoneType' object has no attribute 'cleanup'",
            "Feb 12 10:58:10 ultimakersystem-ccbdd30046a1 python3[450]: 2019-02-12 10:58:10,137 WARNING  printJob        Print of UM3_MusicNoteKeyChainXL could not be started"

     

    All others API ( led, blink, message... ) are working well with digest auth but this one ( maybe because of the multipart/form-data aspect) is not working consistently for me.

     

    Can anyone provide support on handling Print_job api from a webapp (nodejs / python? ) / or a working code?

     

    thanks

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS

    Hello,

    I moved to python3 and here is my working code:

        def StartPrint(parent, file):
            logger.info("Start print job. File: ")
            logger.info(file)
            
            if os.path.exists(file):
                jobname = os.path.basename(file)
                filepath = file
                logger.debug(jobname)
                # Start a print 
                r = requests.post('http://10.10.109.40/api/v1/print_job', auth=requests.auth.HTTPDigestAuth('45fa38b52ea5a998c085145930356bd6', '63bb0fc18a39ba7c787805e80f0be1211144f4083de634deb57f7e7819a640ec'), files={"file": (jobname, open(filepath, "rb"))} )
                logger.info(r.status_code)
            
                if r.status_code == 201:
                    return True
                else:
                    return False
            else:
              logger.info("File doesn't exist!")
              return False

    You have to change the IP and auth data to your device.

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS

    Thank you Korny, works perfectly !

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS
    On 2/12/2019 at 1:01 PM, Greg79 said:

    Hi everybody ! Any update on this one? i'm facing the same result on nodejs.

     

    Moreover : 

    I tried with multiple tools : 

    - Insomnia : works as expected -> It's possible to start a print job

    - Curl command line : works as expected -> It's possible to start a print job

    - Post Man : Cannot make it work ( error code : 400 Bad request :  "message": "No file received")

    - Python3 : same as PostMan ( the file seems to be received by the printer as i see message in printer's log : (but seems empty?)

     

    All others API ( led, blink, message... ) are working well with digest auth but this one ( maybe because of the multipart/form-data aspect) is not working consistently for me.

     

     

    Hi there!

    I am also having trouble with sending a .gcode file to printjob API. i am using postman and have all other APIs working fine, including some that require the digest auth.

    I specify "file" and "jobname" as required. for "file" i use the file type and i select my file from the little file selector window.

    It's only when trying to send to print, I always get the response ""No file received".

     

    P.s - when i try the same call with same file and credentials via the "api documentation" ([IP]/docs/api/) in the chrome browser, it works fine and i can print.

     

    can anyone assist please?

    thanks!

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS

    I have the same issue:

     

    On 6/26/2019 at 9:22 PM, backbone said:

    I am also having trouble with sending a .gcode file to printjob API. i am using postman and have all other APIs working fine, including some that require the digest auth.

    I specify "file" and "jobname" as required. for "file" i use the file type and i select my file from the little file selector window.

    It's only when trying to send to print, I always get the response ""No file received".

     

    P.s - when i try the same call with same file and credentials via the "api documentation" ([IP]/docs/api/) in the chrome browser, it works fine and i can print.

     

    Did you manage to find a solution?

     

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS
    3 hours ago, modernonline said:

    I have the same issue:

     

     

    Did you manage to find a solution?

     

     

    It just seems to not be working with node (even when the code is correct). I finally used my node server to call a python server (just for this api call) and it works. 

     

    I used the python code provided earlier in this thread and this work perfectly. 

  • Link to post
    Share on other sites
    Posted · POST print_job with nodeJS

    Thanks for your reply. In my case it was a faulty request ( I was using Python to start with) due to... hey hey hey! Indentation 🙂

     

     

  • Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    ×
    ×
    • Create New...