POST request empty (body-parser extended: true) - still not working

I am working on retrieving a post request body so that i can add the arguments in the body to my database.

At first I was getting an error SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse (<anonymous>) but i added this line applctn.use(express.urlencoded({ extended: true })); // to support URL-encoded bodies and now i get an empty body.

My code looks like this :

const express = require("express");
const moment = require("moment");
const db = require("./dbconnection.js"); //reference of dbconnection.js
var bodyParser = require("body-parser");

const applctn = express();

applctn.use(bodyParser.urlencoded({extended: true}));

applctn.post("/hl7_message", function(req, res) {

    var jsonObj = JSON.stringify(req.body);

    console.log(req);

When i add app.use(bodyParser.json()); // to support JSON-encoded bodies i get the error ``` SyntaxError: Unexpected token ’ in JSON at position 0 at JSON.parse () ````

Any advise /tips on how i can retrieve the body contents will be appreciated.

Judging by the tiny piece of code you posted and the error message, I’d say that the problem is on the request part.
Show us how you’re calling the endpoint.

And btw, it’s common to use app instead of applctn.

1 Like

I hope i understood your question correctly.

I am getting data from a source A , then i post it to my endpoint which is my source B, and i want to retrieve the request from source B

//source A of data

applctn.post("/hl7_message", function(req, res) {

        "use strict";

        console.log(req)

        var hl7_message = JSON.stringify(req.body);

        connectivity(function(online) {

            if (online) {

                console.log("connected to the internet!");

                winston.log("info", "connected to the internet!", {

                    someKey: "Connection Success",

                });



                log.info(" Connected to the internet! ");


// passing my data to my endpoint so that i can retrive the request in my code above
                var options = {

                    method: "POST",

                    url: "http://xx.xxxx.xx.xx/hl7_message",

                    headers: {

                        "Content-Type": "application/json",

                    },

                    body: JSON.parse(hl7_message),

                    json: true,

                };

                request(options, function(error, response, body) {

                    if (error) throw new Error(error);



                    console.log(body);

                });

                res.send(true);

            } else {

                console.error("sorry, not connected!");



                winston.log("info", " No Internet Connection", {

                    someKey: "No Transmission. ",

                });



                log.info(" No Internet Connection");

                res.send(false);

            }

        });

    });

You’re problem is probably in the way you’re calling B from A. Show that A part that is calling B.

The payload in source A is like this:

{"MESSAGE_HEADER":{"SENDING_APPLICATION":"ADT","SENDING_FACILITY":"13939","RECEIVING_APPLICATION":"IL","RECEIVING_FACILITY":"13939","MESSAGE_DATETIME":"20210312010126","SECURITY":"","MESSAGE_TYPE":"SIU^S12","PROCESSING_ID":"P"},"PATIENT_IDENTIFICATION":{"EXTERNAL_PATIENT_ID":{"ID":"","IDENTIFIER_TYPE":"GODS_NUMBER","ASSIGNING_AUTHORITY":"MPI"},"INTERNAL_PATIENT_ID":[{"ID":"1393926838","IDENTIFIER_TYPE":"CCC_NUMBER","ASSIGNING_AUTHORITY":"CCC"}],"PATIENT_NAME":{"FIRST_NAME":"LETICIA","MIDDLE_NAME":"ALVINS","LAST_NAME":"AUMA"}},"APPOINTMENT_INFORMATION":[{"PLACER_APPOINTMENT_NUMBER":{"NUMBER":288795,"ENTITY":"ADT"},"APPOINTMENT_REASON":"REGIMEN REFILL","APPOINTMENT_TYPE":"PHARMACY APPOINTMENT","APPOINTMENT_DATE":"20210505","APPOINTMENT_PLACING_ENTITY":"ADT","APPOINTMENT_LOCATION":"PHARMACY","ACTION_CODE":"A","APPOINTMENT_NOTE":"TO COME BACK FOR A REFILL","APPOINTMENT_STATUS":"PENDING"}]}

But the payload in source B is like this:

'{\"MESSAGE_HEADER\":{\"SENDING_APPLICATION\":\"KENYAEMR\",\"SENDING_FACILITY\":\"13939\",\"RECEIVING_APPLICATION\":\"IL\",\"RECEIVING_FACILITY\":\"13939\",\"MESSAGE_DATETIME\":\"20210212090359\",\"SECURITY\":\"\",\"MESSAGE_TYPE\":\"SIU^S12\",\"PROCESSING_ID\":\"P\"},\"PATIENT_IDENTIFICATION\":{\"EXTERNAL_PATIENT_ID\":{\"ID\":\"\",\"IDENTIFIER_TYPE\":\"GODS_NUMBER\",\"ASSIGNING_AUTHORITY\":\"MPI\"},\"INTERNAL_PATIENT_ID\":[{\"ID\":\"13939-15477\",\"IDENTIFIER_TYPE\":\"CCC_NUMBER\",\"ASSIGNING_AUTHORITY\":\"CCC\"}],\"PATIENT_NAME\":{\"FIRST_NAME\":\"JOHN\",\"MIDDLE_NAME\":\"OTIENO\",\"LAST_NAME\":\"LUSI\"},\"MOTHER_NAME\":{\"FIRST_NAME\":\"\",\"MIDDLE_NAME\":\"\",\"LAST_NAME\":\"\"},\"DATE_OF_BIRTH\":\"\",\"SEX\":\"\",\"PATIENT_ADDRESS\":{\"PHYSICAL_ADDRESS\":{\"VILLAGE\":\"\",\"WARD\":\"\",\"SUB_COUNTY\":\"\",\"COUNTY\":\"\",\"GPS_LOCATION\":\"\",\"NEAREST_LANDMARK\":\"\"},\"POSTAL_ADDRESS\":\"\"},\"PHONE_NUMBER\":\"\",\"MARITAL_STATUS\":\"\",\"DEATH_DATE\":\"\",\"DEATH_INDICATOR\":\"\",\"DATE_OF_BIRTH_PRECISION\":\"\"},\"APPOINTMENT_INFORMATION\":[{\"APPOINTMENT_REASON\":\"\",\"ACTION_CODE\":\"A\",\"APPOINTMENT_PLACING_ENTITY\":\"KENYAEMR\",\"APPOINTMENT_STATUS\":\"PENDING\",\"APPOINTMENT_TYPE\":\"\",\"APPOINTMENT_NOTE\":\"N/A\",\"APPOINTMENT_DATE\":\"20210507\",\"PLACER_APPOINTMENT_NUMBER\":{\"ENTITY\":\"KENYAEMR\",\"NUMBER\":\"\"}}]}'

Looks like you’re not stringifying your JSON in A before sending to B.
Can you finally show the code in A, that is calling B?

I have a consumer and receiver. My consumer gets data from my source hl7_message, and post it to my endpoint http://xx.xxxx.xx.xx/hl7_message. From there i have a receiver, which takes data from http://xx.xxxx.xx.xx/hl7_message and post it to my db.

This code is for my consumer, that takes data from source A to B

//source A of data

applctn.post("/hl7_message", function(req, res) {
        "use strict";

        console.log(req)

        var hl7_message = JSON.stringify(req.body);

        connectivity(function(online) {

            if (online) {

                console.log("connected to the internet!");

                winston.log("info", "connected to the internet!", {

                    someKey: "Connection Success",

                });



                log.info(" Connected to the internet! ");


// passing my data from source A to B via an endpoint. This endpoint will be used in my rceiver below
                var options = {

                    method: "POST",

                    url: "http://xx.xxxx.xx.xx/hl7_message",

                    headers: {

                        "Content-Type": "application/json",

                    },

                    body: JSON.parse(hl7_message),

                    json: true,

                };

                request(options, function(error, response, body) {

                    if (error) throw new Error(error);



                    console.log(body);

                });

                res.send(true);

            } else {

                console.error("sorry, not connected!");



                winston.log("info", " No Internet Connection", {

                    someKey: "No Transmission. ",

                });



                log.info(" No Internet Connection");

                res.send(false);

            }

        });

    });

This is now the code for my receiver, that takes data from source B, and attempts to post it to my db

const express = require("express");
const moment = require("moment");
const db = require("./dbconnection.js"); //reference of dbconnection.js
var bodyParser = require("body-parser");

const applctn = express();

applctn.use(bodyParser.urlencoded({extended: true}));

applctn.post("/hl7_message", function(req, res) {

//this req is what comes with an empty body so i have nothing to post to my db

    console.log(req);
}
body: JSON.parse(hl7_message)

should be

body: JSON.stringify(hl7_message)

But i already converted the source A to a string? Could you shed some more light on why i need to convert it again, or recommend something i can read, kindly?

Ok, I missed that line where you did that.
Then it should be just:

body: hl7_message