简体   繁体   中英

Receving POST request from Node JS server on Arduino

Im trying to send a post request to an arduino with Node JS and the Request package:

  var body = {
    d:"5",
    l:"6",
    TOTAL_VOLUME: "75",
    meterId: "9"

  };
var options = {
    url: 'http://'+'192.168.1.102'+'/configData',
    timeout: 7000,
    headers: {
      'Content-type' : 'application/json',
      'Content-length': JSON.stringify(body).length
    },
    json:true,
    body: JSON.stringify(body)
  };

  request.post(options, function (error, response, body) {
    //console.log(error);
    //console.log(response);
    console.log(body);
    if (!error && response.statusCode == 200) {
      console.log("Changed configuration succesfully. ");
      // Request to enpoint to save changes in database
      var options = {
        url: 'http://'+'8.8.8.8:4000'+'/meter/'+meter.id+'/',
        method: 'PUT',
        timeout: 10000,
        body: {
          'tank_diameter': tank_diameter,
          'tank_length':tank_length,
          'tank_capacity': tank_capacity
        }
      };

      /*request(options, function (error, response, body) {
       if (!error && response.statusCode == 200) {

       }

       });*/

    }
    done();
  }).on('error', function(err) {
    console.log(err);
    done();

  });

The above code is how I send the data, However Im not able to get the data on the arduino.

This is the code on arduino:

server.on("/configData", HTTP_POST, [](){                       // configData Seteo de Valores desde POST
    StaticJsonBuffer<200> configBuffer;
    JsonObject& configJson= configBuffer.parseObject(server.arg("plain"));
    String l = configJson["l"];
    String d = configJson["d"];
    String meterId2 = configJson["meterId"];
    String volumenTotal = configJson["TOTAL_VOLUME"];
    LENGTH = l.toFloat();
    HEIGHT = d.toFloat();
    meterId = meterId2.toInt();
    TOTAL_VOLUME = volumenTotal.toFloat();
    // GUARDAR EN LA EEPROM
    int EEadr = 0;
    EEPROM.write(EEadr, HEIGHT);
    EEPROM.commit();
    EEadr = 10;
    EEPROM.write(EEadr, LENGTH);
    EEPROM.commit(); 
    EEadr = 20;
    EEPROM.write(EEadr, TOTAL_VOLUME);
    EEPROM.commit(); 
    EEadr = 30;
    EEPROM.write(EEadr, meterId);
    EEPROM.commit();

    //SHOW ON SERIAL MONITOR   
    Serial.println("l= "+l);
    Serial.println("d= "+d);
    Serial.println("meterId2= "+meterId2);
    Serial.println("TOTAL_VOLUME= "+volumenTotal);
    server.send ( 200, "text/json", "{success:true}" );
  });  

The weird thing is that if I use curl like this:

curl -H "Content-type: application/json" -X POST -d "{l:\"55\", r:\"10\", meterId: \"2\"}" http://192.168.1.2

The arduino does receive the data correctly, so the problem is most likely on my Node JS request. Can anyone tell me what Im I doing wrong here?

UPDATE:

Ive checked the requests with wireshark, and it results that the curl request (the one that is working) is being sent as Line based text data. Can anyone tell me how can I send it the same way using Node JS and request? 在此处输入图片说明

In these type of situations you can check your request structure with applications like wireshark .

In this problem if you can see that you attach your hole json as a single string, because when you set json flag of request in request library it convert your body into json for you so now you have something like:

var options = {
  body: JSON.stringfy(JSON.stringfy(body))
};

so you can correct your application by simply set following options:

var options = {
  url: 'http://'+'www.goole.com'+'/configData',
  timeout: 7000,
  json:true,
  body: body
};

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