Search code examples
javascriptnode.jsexpressserverhttprequest

Is there a way to take values out of this request body?


I am using node.js and express to receive post requests from another service that I do not manage. The request body returns in the console as follows.

{
  '{"org_id":"TSTORG","network_id":0,"ts":"2021-09-01T17:40:24","device_name":"Test Device","device_id":0,"ssid":"Test SSID","client_name":"Test Client Device","client_mac":"AA:BB:CC:DD:EE:FF","event_type":"WLAN","detail":"Event log notification test","longitude":0.1,"latitude":0.2,"gps_timestamp":"2021-09-01T17:40:24","sn":"0000-0000-0000"}': ''
}

It seems to be a JSON Object with a JSON string inside of it. Is this request malformed or am I missing something? Thanks in advance.


Solution

  • I eventually figured this out.

    var data = request.body;
    var key = Object.keys(data)[0];
    

    I had to use the Object.keys method to return just the text inside the object. From there:

    var p = JSON.parse(key)
    console.log(p)
    

    This allowed me to reform the JSON string I got back into a workable JSON format that I could extract values from.