Http Request to send data

Hello,

I need to send data in a specific format via http, like this: http://192.168.X.X/apienergia/funcions.php?funcio=updateComptadorGET&tag=X&valor=X&datahora=X , where tag, valor and datahora are the variables that I need to send.

I'm doing what you can see in the image, and as you can see it doesn't send correctly (msg.payload : string [0])

Somebody knows which could be the problem?

Thank you,
Roger.

Hello .. the problem is not very clear .. so you get a reply back as an empty string ?
it all depends on your device's documentation .. do you have a link ?

also have you tried in the Function node (just before your http-request)

let tag = msg.payload.deveuil;
let valor = msg.payload.Comptador;
let datahora = msg.payload.DataHora;

msg.url = `http://192.168.X.X/apienergia/funcions.php?funcio=updateComptadorGET&tag=${tag}&valor=${valor}&datahora=${datahora}`
msg.method = "POST"

return msg;

ps. in the test, clear the url from the http-request node since we are sending it from the function

Hello UnborN,

Yes, I have an empty string. All that I need to do is reply this http://192.168.X.X/apienergia/funcions.php?funcio=updateComptadorGET&tag=X&valor=X&datahora=X with my data.

I put like what you explain but it happens the same because it doesn't detect the variables.

Thank you,
Roger.

Do you have any Documenation for the device ?

It is an api with this code:

imagen

I don't know anything else because it's from other teammate.

Thank you,
Roger.

dont know php but that function tries to get the url parameters and if funcio is updateComptador

BUT you are sending?funcio=updateComptadorGET&tag=X ..
maybe that extra GET in updateComptadorGET is the problem ?

try .. ?funcio=updateComptador&tag=X ..

[EDIT]

Something to try

let tag = msg.payload.deveuil;
let valor = msg.payload.Comptador;
let datahora = msg.payload.DataHora;

msg.url = `http://192.168.X.X/apienergia/funcions.php?funcio=updateComptador`
msg.method = "POST"
msg.headers = { "Content-type": "application/x-www-form-urlencoded" }

msg.payload = { "tag": tag, "valor": valor, "datahora": datahora }

return msg;

I'm trying and trying so many options but I can achieve it. With your function UnborN now I have the next:

Any more idea on how I can send this data to this API rest with Node-RED, I don't know what else to do.

Very gratefull,
Roger.

You have to study the PHP code and see what it does
a quick search for $_POST

An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

We tried urlencoded .. try multipart/form-data
im not sure of the syntax but there is an example at the end of the http-request node's help tab.

maybe ..

let tag = msg.payload.deveuil;
let valor = msg.payload.Comptador;
let datahora = msg.payload.DataHora;

msg.url = `http://192.168.X.X/apienergia/funcions.php?funcio=updateComptador`
msg.method = "POST"
msg.headers = { "Content-type": "multipart/form-data" }

msg.payload = {
    "tag": { "value": tag },
    "valor": { "value": valor },
    "datahora": { "value": datahora }
}

return msg;

ps1. also check why it returns a 404 .. do you have the IP and url correct ?
ps2. are you sure its a POST request .. maybe its method is GET ?
it not common and usually request that send data are POST but worth to try.

Good morning,

Thanks for all your help UnborN, so sorry, the developer jnow says me that I need to to a GET.

As far as i can see from your earlier post,

and your PHP screenshot...

... you simply need to create a URL with query parameters.

let tag = msg.payload.deveuil;
let valor = msg.payload.Comptador;
let datahora = msg.payload.DataHora;

msg.url = `http://192.168.X.X/apienergia/funcions.php?funcio=updateComptador&tag=${tag}&valor=${valor}&datahora=${datahora}`
msg.method = "GET"

//Should be no harm including values in payload...
msg.payload = { "tag": tag, "valor": valor, "datahora": datahora }
//... but you can probably delete the above line if needed

return msg;

Send the output of the function to a HTTP Request node (leave the URL field blank so that it is populated by the value generated in msg.url

Also, send the output of the function to a debug node (set to show complete msg) and show us what you get in the debug sidebar.

Hello Steve,

By one hand:

By the other hand:

You didn't use the code I wrote. It still has GET in the middle. And, the code for the url string is a regular string '' instead of a template string ``

Also, can you put a debug BEFORE the function so we can see what is in msg.payload.

Sorry Steve, I have little knowledge programing and I thinked that it was the same. Now it's working well but with the "GET" in the middle.

I attach de solution if it's usefull for somebody.

Many thanks Steve and UnborN for all your help.

I dont think its working because of GET in the url .. ?funcio=updateComptadorGET ..
you get a reply statusCode 200 because the msg.method is "GET"

but have you confirmed that the PHP executed as it should ?
the php code you shared says if funcio === "updateComptador"
so if you have in url .. ?funcio=updateComptadorGET .. i wonder if it executes

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