Http parser device

Good morning … can you help me? I need a code to convert the data from my http device:

07:47:28:

[POST] HTTP Request:

“From: 177.160.99.30 [EHS6/358211100251769 Profile/IMP-NG Configuration/CLDC-1.1] - Content-Type: text/plain - Token Ending: ffa7a”

07:47:28:

Raw payload:

“00004952;85;11/09/20;07:45:00;DI=0000000000000001;EV=0000000000000000;TP1=33974;TP2=0;”

The data I need to write to my bucket are the values of “TP1” and “TP2”. I cannot develop this code. The system is responding to me with an error message:

07:47:28:

Error on [device] payload parser:

{}

I appreciate if you can help me. I apologize for my bad English !!!

Hi, @leandro.

Here is a simple payload parser that I think will help you.

// let payload = "00004952;85;11/09/20;07:45:00;DI=0000000000000001;EV=0000000000000000;TP1=33974;TP2=0;"

// Payload is an environment variable. Is where what is being inserted to your device comes in.
if (payload) {
  // Get a unique serie for the incoming data.
  const serie = new Date().getTime();

  // Parse the payload to Array of Objects format (it comes in a String format)
  if (typeof payload === 'string') {
    const vars = payload.split(";");
    payload = [];
    for (const item of vars) {
      const [key, value] = item.split("=");
      if (!value) continue;
      payload.push({
        variable: key,
        value,
        serie,
      });
    }
  } else {
    return console.log('Payload is not text/plain type.')
  }

  payload = payload.filter(item => item.variable === 'TP1' || item.variable === 'TP2');
}

Let me know if you need any help.

Thank you very much, everything went well

1 Like