Send a simple JavaScript

Hi everyone!!! I'm new to this forum and to using node-red

I'm trying everything but i cannot get this simple javascript code to work

var xhr = new XMLHttpRequest();
xhr.open("POST", "...url...", true);

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
}
}
xhr.send("name=pippo&surname=topolino&street=blablabla");

Which NODE should be used ??
Thanks for your help!!!

Welcome to the forum @Colla

Is there a reason you don't want to use the http request node?

Thanks @Colin!

No, there isn't reason, but I already have the code in javascript ready

This should help...

Ok but it's not clear to me how to integrate it with my javascript code

Best to use the node, that is the node-red way.

Have you tried the cook book examples?

Also,

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Yes I looked at the examples, but they didn't help me

I also watched part of the NODE RED ESSENTIAL videos

I thought it was an easy thing to do. I'll do some more tests and document myself better

Do you mean you were not able to work out how to perform the http request using that node?

exact...
I think it's a "stupid" thing but I'm not succeeding!!!

Hi @Colla

here is an example flow that should help you get started:

[{"id":"923bc595.5cc568","type":"http request","z":"d607ce33.4fa5a","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":660,"wires":[["98fe22d2.28da2"]]},{"id":"b0a97530.5a0608","type":"inject","z":"d607ce33.4fa5a","name":"","props":[{"p":"payload"},{"p":"headers","v":"{\"content-type\":\"application/x-www-form-urlencoded\"}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"name\":\"pippo\",\"surname\":\"topolino\",\"street\":\"blablabla\"}","payloadType":"json","x":390,"y":660,"wires":[["923bc595.5cc568"]]},{"id":"98fe22d2.28da2","type":"debug","z":"d607ce33.4fa5a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":660,"wires":[]}]

Copy that JSON then import it into your Node-RED Editor using the "Import" menu option.

That will give you a flow that looks like this:

The Inject node sets two properties on the message:

msg.payload is set to a Object containing the data you want to send in the request:

{"name":"pippo","surname":"topolino","street":"blablabla"}

msg.headers is set to an Object containing the HTTP headers to use on the request:

{"content-type":"application/x-www-form-urlencoded"}

Next the HTTP Request node will do the actual request. I have configured it already to do a POST request - but you'll need to fill in the URL.

The result of the request will be passed to the final Debug node to display the result in the Debug Sidebar.

Once you've imported that to your workspace and set the URL property of the request node, hit the Deploy button, then trigger the Inject node by clicking its button.

I hope that's good enough to help you get started.

Hi @knolleary!

Thank you so much for the help and explanation!!
Now it's much clearer how it works,

I was wrong to send the request correctly in the header: {"content-type":"application/x-www-form-urlencoded"}

Really thanks again for the support!!

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