Search code examples
jsonhttpcurlnode-red

Node-red - Posting data to influxdb via http


Im trying to post data to an Influxdb via Node-red. Via CURL i can post this:

curl -i -XPOST 'http://localhost:8086/write?db=waterlevel' --data-binary 'vattenstand,lake=siljan,region=dalarna value=160.80'

and it puts data to InfluxDb.

When I try to post via Node-red and an HTTP request I get the error:

{"error":"unable to parse '{\"url\":\"http://192.168.1.116:8086/write?db=waterlevel\",\"method\":\"POST\",\"body\":\"vattenstand,lake=siljan,region=dalarna value=160.80\",}': missing tag value"}

I use this code in a function in Node-red and pass it to the HTTP request:

var dataString = 'vattenstand,lake=siljan,region=dalarna value=160.80';

msg.payload = {
    'url': 'http://192.168.1.116:8086/write?db=waterlevel',
    'method': 'POST',
    'body': dataString,
};

msg.headers = {
    Accept: "application/json"
};

return msg;

enter image description here

enter image description here


Solution

  • The sidebar help for the node details the msg properties you should be setting to configure the node.

    You are passing in URL, method and body as properties of msg.payload. That is not correct.

    They should be set as msg.url, msg.method for the first two, and msg.payload should be the body of the request.

    In this instance, you have already configured the node with a URL and method directly, so there's no need to pass them in with the message. In fact, as you have configured the URL in the node you will find you cannot override it with msg.url. if you want to set the URL with each message, you must leave the node's URL field blank in the editor.

    You may also need to set the content-type header.

    Assuming you are happy to leave the URL and method hard coded in the node, you function should be something like:

    msg.payload = 'vattenstand,lake=siljan,region=dalarna value=160.80';
    
    msg.headers = {
        Accept: "application/json"
    };
    msg.headers['Content-type'] = 'application/x-www-form-urlencoded';
    
    return msg;