HTTP POST in node-red js

I want to know how to make an HTTP POST request with JSON data to some server with an API key. I searched at http://nodered.org/docs/ But they didn't write anything. The documentation is very messy and not even clear. However, I tried to POST the JSON data which is gated:

{"version":"1.0.1","sensors":[{"sensor":"accel","output":[{"name":"accelert","type":"dcmotion"}]}]}

      

I wrote the API in a node function since (the API is not here, not original)

var msg = {"version":"1.0.1","sensors":[{"sensor":"accel","output":[{"name":"accelert","type":"dcmotion"}]}]}

msg.headers: {
                    'x-api-key': 'ucasdfeacceacxfAIH2L4=',
                    'content-type': 'application/json"
                }

      

I got this example from here: https://groups.google.com/forum/#!msg/node-red/nl9Be0dN55g/S_VYMTjOanEJ

And I added the node input as an HTTP POST, then gave the url and linked it with the added debug function node. Now I unrolled it. I am getting the error: Unexpected token in API node

Now I'm not sure how to do this. I don't understand how to do this. Please help me. There is no tutorial on the node red site.

+3


source to share


2 answers


Instead var msg = {... you must use msg.payload = {... .



Since msg is a standard JSON object message passed between node-red nodes, so should not be declared with var and its payload property contains the message body, so when msg is provided for an HTTP request node , the payload property is sent as the request body (see the HTTP request node info tab ).

+1


source


From your follow-up question on this topic, I see that you have overcome the problems you had here.

To repeat the answer I gave there, the payload you want to post must be in the payload property of the object you are returning from the function. The http request node documentation describes it all.



You will find that there is currently not much activity in node-red on Stack Overflow. Hopefully this changes over time, but for now you will find the mailing list much more responsive.

Also, if you have specific feedback, we would welcome it on the mailing list.

+1


source







All Articles