3d printer api doesn't work

So I have following code to send gcode to my ender 3:

rule "POST System Kommando"
	Item OctoprintPrinterCommand received update
var String PrinterCmd = OctoprintPrinterCommand.state.toString()
    var url = "http://octopi/api/job?apikey=`XXXXXXXXXXXXXXX"
    var contenttype = "application/json"
    var POSTrequest = '{"command":"'+PrinterCmd+'"}'
    logInfo(filename, "Ender3 kompiliert GCode: " + PrinterCmd )

    var output = sendHttpPostRequest(url, contenttype, POSTrequest)
    logInfo(filename, output);

the json api looks like this:

But the logs are showing the following when I do a HTTP post with “G28” (Auto home) to my printer:

2018-12-08 19:48:11.203 [INFO ] [arthome.model.script.Octoprint.rules] - Ender3 kompiliert GCode: G28

2018-12-08 19:48:11.262 [INFO ] [arthome.model.script.Octoprint.rules] - Expected valid command

Is my syntax incorrect ? Does the command maybe need to have those
inverted commas (")? if yes - how do I type them, without comment out my Code?

I think? that you need to add double quotes to your POSTrequest (and maybe escape the double quotes within the content also)

try this:

var POSTrequest = "{\"command\":\"+PrinterCmd+\"}"

see example here: JSONPATH updating Item state

edit: you many need to play with single and double quotes to get the PrinterCmd working… my proposal may not pull correctly the PrinterCmd var… I have to re-think the syntax

1 Like

thank you!
give me a second :slight_smile:

1 Like

new Code:

    var POSTestRequest = "{\"command\":\"+PrinterCmd+\"}"
    logInfo(filename, "Ender3 kompiliert GCode: " + PrinterCmd )
    logInfo(filename, POSTestRequest)
    //Antwort der API
    var output = sendHttpPostRequest(url, contenttype, POSTestRequest)
    logInfo(filename, output);


2018-12-08 20:32:35.921 [INFO ] [arthome.model.script.Octoprint.rules] - Ender3 kompiliert GCode: G28
2018-12-08 20:32:35.936 [INFO ] [arthome.model.script.Octoprint.rules] - {"command":"+PrinterCmd+"}
2018-12-08 20:32:36.004 [INFO ] [arthome.model.script.Octoprint.rules] - Expected valid command

still the same behaviour…
If it helps, the old Code gave out the following

var POSTrequest = '{"command":"'+PrinterCmd+'"}':
2018-12-08 20:31:39.052 [INFO ] [arthome.model.script.Octoprint.rules] - {"command":"G28"}

I don’t understand why your original rule is not working…
I checked 3D Printer Status and Commands from/to Octoprint via HTTP REST API also and I see that your syntax is correct. It seems that you don’t need the double quotes…

I am going to try to reproduce this on my system for deeper validation and let you know what I find.

1 Like


Could you please ensure that you select a category for your posts?

I would be really grateful, if you take the time to do this :slight_smile:
Thank you alot!

Yes, sorry!

var POSTestRequest = "{\"command\":\"+PrinterCmd+\"}"

I think that should be

var POSTestRequest = "{\"command\":\"" + PrinterCmd + "\"}"

still getting:

2018-12-09 10:45:12.067 [INFO ] [arthome.model.script.Octoprint.rules] - {"command":"G28"}

2018-12-09 10:45:12.146 [INFO ] [arthome.model.script.Octoprint.rules] - Expected valid command

this is weird, the syntax should be correct …

are you referencing a Epson printer?
I was talking about my ender 3, and everything worked in the end

I think it just wants you to click its link

many times is happening with my HP printer that printer is connected with the devices but HP printer not printing. printer not printing anything so that reasons could be anything. there is a basic mistake that we made so the occurred these issues, I know about the all basic factors from the HP tech support.