Octoprint rest API

Did anybody made some work with octoprint rest API?

I'm monitoring some data through MQTT but having rest API is plenty f integration possibilities however I have no idea how that works and will be nice to have some flow example to start with.

Thanks in advance

I guess should be something like this...



[{"id":"4106d02f.a1315","type":"debug","z":"4c5509ca.d3c3a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":1480,"wires":[]},{"id":"7a4d8750.810318","type":"inject","z":"4c5509ca.d3c3a8","name":"","topic":"","payload":"/api/printer/tool?history=true&limit=2","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1480,"wires":[["6cab1195.8fbfa"]]},{"id":"6cab1195.8fbfa","type":"http request","z":"4c5509ca.d3c3a8","name":"","method":"GET","ret":"obj","url":"http://192.168.1.156?api_key=C69966D056AF4ED88F48ADB119CF16B1 ","tls":"","x":480,"y":1500,"wires":[["4106d02f.a1315"]]}] 

image

However the response I get is:

msg : Object
object
_msgid: "1494f902.40e927"
topic: ""
payload: string
<html>
<head>
    <title>OctoPrint Login</title>

    <!-- le CSS -->

<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="/static/vendor/font-awesome-4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="/plugin/forcelogin/static/css/forcelogin.css">



    <!-- le javascript -->

<script>
    var BASE_URL = "/";
</script>
<script src="/static/js/lib/jquery/jquery.min.js"></script>
<script src="/static/js/lib/sockjs.min.js"></script>
<script src="/static/js/lib/bootstrap/bootstrap.js"></script>
<script src="/static/js/lib/lodash.min.js"></script>


    <script type="text/javascript" src="/static/webassets/packed_client.js?4965268c"></script>


<script src="/plugin/forcelogin/static/js/main.js"></script>
</head>
<body>
    <div class="container">
        <form class="form-signin">
            <h2 class="form-signin-heading">Please log in</h2>

            <div id="login...
statusCode: 200
headers: object
content-length: "2095"
x-robots-tag: "noindex, nofollow, noimageindex"
set-cookie: array[1]
last-modified: "Wed, 30 Jan 2019 00:19:14 GMT"
etag: ""fc0f9581859fa4f96170ce4bef53bd29b18bc096""
x-from-cache: "true"
x-clacks-overhead: "GNU Terry Pratchett"
content-type: "text/html; charset=utf-8"
connection: "close"
x-node-red-request-node: "4a0648d3"
responseUrl: "http://192.168.1.156/?api_key=C69966D056AF4ED88F48ADB119CF16B1"
responseCookies: object
session_P80: object

As per WIKI http://docs.octoprint.org/en/master/api/printer.html#retrieve-the-current-tool-state

I was expecting to get something like:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "tool0": {
    "actual": 214.8821,
    "target": 220.0,
    "offset": 0
  },
  "tool1": {
    "actual": 25.3,
    "target": null,
    "offset": 0
  },
  "history": [
    {
      "time": 1395651928,
      "tool0": {
        "actual": 214.8821,
        "target": 220.0
      },
      "tool1": {
        "actual": 25.3,
        "target": null
      }
    },
    {
      "time": 1395651926,
      "tool0": {
        "actual": 212.32,
        "target": 220.0
      },
      "tool1": {
        "actual": 25.1
      }
    }
  ]
}

Frankly speaking I have no idea how that works and I'm trying to get some point to start with.
Regards

Try setting the HTTP request node to GET and the URL...

http://192.168.1.156?api_key=C69966D056AF4ED88F48ADB119CF16B1/api/printer/tool?history=true&limit=2

3 edits later my post says what I meant lol

same result

I made a mistake (4th one doh)

Try...

http://192.168.1.156/api/printer/tool?history=true&limit=2&api_key=C69966D056AF4ED88F48ADB119CF16B1

Ps I've never used this device or interface however what I can say is you are seeing a web page rather than the reply from a http end point. This should tell you the URL is formatted incorrectly or your pointing to wrong port.

Usually it's
http://address/path/path?parameter=value

Set the Api key as msg.header. That works for me.

[
    {
        "id": "faec932f.609f9",
        "type": "http request",
        "z": "1113cc41.f63e74",
        "name": "",
        "method": "use",
        "ret": "obj",
        "paytoqs": false,
        "url": "http://3dprinter.url/{{{command}}}",
        "tls": "",
        "proxy": "",
        "x": 550,
        "y": 180,
        "wires": [
            []
        ]
    },
    {
        "id": "d42f6979.459bd8",
        "type": "function",
        "z": "1113cc41.f63e74",
        "name": "Api",
        "func": "var stat = global.get(\"3DPRINTER\");\n\nif (msg.topic) {\n    msg.topic = msg.topic.toLowerCase();\n\n    msg.headers = {\n             \"X-Api-Key\" : \"C5346236654563350338C516ABF\"\n        };\n\n    switch (msg.topic) {\n    \tcase \"off\":\n\t\t    msg.command = \"/api/system/commands/core/shutdown\";\n            msg.method = \"POST\";\n\t\t    return msg;\n\t    case \"reboot\":\n    \t\tmsg.command = \"/api/system/commands/core/reboot\";\n            msg.method = \"POST\";\n\t\t    return msg;\n\t    case \"restart\":\n    \t\tmsg.command = \"/api/system/commands/core/restart\";\n            msg.method = \"POST\";\n\t\t    return msg;\n\t    case \"restart_safe\":\n    \t\tmsg.command = \"/api/system/commands/core/restart_safe\";\n            msg.method = \"POST\";\n\t\t    return msg;\n\t    case \"status\":\n    \t\tmsg.command = \"/api/printer\";\n            msg.method = \"GET\";\n\t\t    return msg;\n\t    default:\n\t\t\t    return null; \n    }     \n}\n",
        "outputs": 1,
        "noerr": 0,
        "x": 350,
        "y": 180,
        "wires": [
            [
                "faec932f.609f9"
            ]
        ]
    }
]
2 Likes

Many thanks!!!!!!!!!! That works, amazing!!!!!!