简体   繁体   中英

node-red.js HTTP POST with API key

I am trying to POST JSOn data to a cloud with this function in function node.

        var accx = 15;
        var accy = 45;
        var accz = 12;

        //JSON FORMING BY JAVASCRIPT OBJECT

        var output = [];
        output[0] = {
            name: "Accel_X",
            value: accx.toString(), // retrieve x
        };
        output[1] = {
            name: "Accel_Y",
            value: accy.toString(), // retrieve y
        };
        output[2] = {
            name: "Accel_Z",
            value: accz.toString() // retrieve z
        };

        var record = [];
        record[0] = {
            starttime: formatDate(new Date()),
            output: output,
        };

        var observations = [];
        observations[0] = {
            sensor: "C2105",
            record: record,
        };

        var fromData = {};
        fromData.version = "1.0.1";
        fromData.observations = observations;


        //MONTH NAME FUNCTION

        function show_now(){
        var my_month=new Date()

        var month_name=new Array(12);
        month_name[0]="JAN"
        month_name[1]="FEB"
        month_name[2]="MAR"
        month_name[3]="APR"
        month_name[4]="MAY"
        month_name[5]="JUN"
        month_name[6]="JUL"
        month_name[7]="AUG"
        month_name[8]="SEP"
        month_name[9]="OCT"
        month_name[10]="NOV"
        month_name[11]="DEC"
        return month_name[my_month.getMonth()];

        }


        //RETURN DATE AT FORMATTED WAY THAT IS ACCEPTED BY CLOUD

        function formatDate(d) {
          return d.getDate() + '-' + (show_now()) + "-" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + " IST";
        }


        var fromDatan = JSON.stringify(fromData);
        //alert(fromDatan);

        //POST JSON SENSOR DATA

                fromDatan.headers = {
                    "x-api-key": "ucrFr234r23rrxfAIH2L4=",
                    "content-type": "application/json;charset=UTF-8"
                }

            return fromDatan;

I have given correct url in url node but it is returning no response and I am not seing any data is being post Anyone please worked with node-red.js please help.

Your function node needs to return an object with a payload property containing the data you want to send in the http request. It is also not necessary to stringify the object as the http request node will do that for you.

    var fromDatan =  {};
    fromDatan.payload = fromData;
    fromDatan.headers = ...

    return fromDatan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM