Sending POST HTTP-Request in JSON for REST-API

Hello Guys,

i'm new to the Node-RED Community, and also new with the scripting and logical connecting of Nodes. Hope we are having a nice time together.

I'm actually doing an project to get a degree as a state-certified technician in Germany.
My task is to connect a collaborating robot (working with REST-API via different HTTP-Requests like POST, PUT, GET, DELETE...) with NodeRED.
Here an Overview from the API (by the manufracturer website)
Overview Choreograph REST API
Nearly all of the requests i did in the NodeRED flow work... excepted this one, where the toolpath gets started by the "controls" unit.

I'm trying to set up a HTTP-request, with payload formatted in JSON.
This is happening in the following flow:

[{"id":"3b67977c.a53c08","type":"debug","z":"84e0de5f.c40b1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":820,"wires":[]},{"id":"4030c52c.c2c29c","type":"inject","z":"84e0de5f.c40b1","name":"Start Toolpath","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":820,"wires":[["31727a2c.05a026"]]},{"id":"64d9243f.b7be7c","type":"http request","z":"84e0de5f.c40b1","name":"HTTP-REQUEST","method":"use","ret":"obj","paytoqs":"ignore","url":"http://eva-perfectbeijingmiller.local/api/v1/controls/run","tls":"","persist":false,"proxy":"","authType":"","x":550,"y":820,"wires":[["3b67977c.a53c08"]]},{"id":"31727a2c.05a026","type":"function","z":"84e0de5f.c40b1","name":"SETTING REQUEST","func":"msg.method = \"POST\"\nmsg.url = \"http://eva-perfectbeijingmiller.local/api/v1/controls/run\"\nmsg.headers = {}\nmsg.headers[\"content-type\"] = \"application/json\"\nmsg.headers[\"Authorization\"] = \"Bearer \"+ flow.get('token');\n\nvar teach = teach\nvar loop = loop\nmsg.payload = [{\n    \"mode\":teach,\n    \"loop\":1\n}]\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":820,"wires":[["64d9243f.b7be7c"]]}]

I already triggered the flow through the timestamp. Here is my answer by the debug node on the right field:

eva_fehler

it seems like NodeRED and the REST-API aren't speaking the same JSON.

There is another noticeable thing...
when im sending the note without setting these definitions:

var teach=teach
var loop=loop

the debug node says:

7.4.2021, 21:22:10 node: SETTING REQUEST
function : (error)
"ReferenceError: teach is not defined (line 7, col 12)"

Trying the wildest constelations of setting '' quotes and using different brackets, but there was no success.

Thanks for your help guys, im really despaired already.
friendly greetings from Germany
Nils

The payload you set as an array with a single object , but the docs show it as an object. Also you did not put quotes around the string "teach"
try

var teach = "teach"
var loop = 1
msg.payload = {
    "mode": teach,
    "loop": loop
}

Hey,
thanks for your fast answer.
I think i already tried this combination of quotes... but not sure.
Unfortunately I can make changes in connection with the robot again on Saturday at the earliest.. But i will give you Information about this solution approach then.

But is there no ability to give NodeRED the Information to ignore the formating of the payload?

Not sure what you mean, the payload has to be formatted as a json, as the api describes. The error message from the response tell you it found [ but expected { .

Okay that sounds quite possible..
I'll try this at first at saturday:

.. when there is no success i try to set the payload like in this example:
msg.payload = {{"mode": teach, "loop": loop}}
will it show a difference? what do you mean?

Hello..
Just wanted to give you a litte update.
The solution with variables in ""quotes worked very fine.
There were no problems with Nodered or the Rest API. Thank you very much :slight_smile:

I'm having some other bugs now in the API which make it hard to work with it...

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.