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

**URL:** <https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [March 19, 2021, 5:11pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962 "2021-03-19T17:11:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![shiro1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shiro1/32/153306_2.png) [@shiro1](https://forum.freecodecamp.org/u/shiro1)\
**Post date:** [March 19, 2021, 5:11pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/1 "2021-03-19T17:11:11Z")

</div>

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 :

```auto
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.

 ![index](https://global.discourse-cdn.com/freecodecamp/original/3X/7/5/755737c486a77bc71cf5587cc8ea35dfc8c94069.jpeg)

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [March 19, 2021, 6:03pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/2 "2021-03-19T18:03:44Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![shiro1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shiro1/32/153306_2.png) [@shiro1](https://forum.freecodecamp.org/u/shiro1)\
**Post date:** [March 19, 2021, 6:27pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/3 "2021-03-19T18:27:36Z")

</div>

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

```auto
//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);

            }

        });

    });

```

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [March 19, 2021, 6:30pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/4 "2021-03-19T18:30:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shiro1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shiro1/32/153306_2.png) [@shiro1](https://forum.freecodecamp.org/u/shiro1)\
**Post date:** [March 19, 2021, 6:49pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/5 "2021-03-19T18:49:13Z")

</div>

The payload in source A is like this:

```auto
{"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:

```auto
'{\"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\":\"\"}}]}'

```

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [March 19, 2021, 6:53pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/6 "2021-03-19T18:53:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shiro1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shiro1/32/153306_2.png) [@shiro1](https://forum.freecodecamp.org/u/shiro1)\
**Post date:** [March 19, 2021, 7:12pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/7 "2021-03-19T19:12:25Z")

</div>

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](http://xx.xxxx.xx.xx/hl7_message). From there i have a receiver, which takes data from [http://xx.xxxx.xx.xx/hl7\_message](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

```auto
//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

```auto
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);
}

```

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [March 19, 2021, 7:16pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/8 "2021-03-19T19:16:15Z")

</div>

```auto
body: JSON.parse(hl7_message)

```

should be

```auto
body: JSON.stringify(hl7_message)

```

---

<div class="post-metadata">

**Author:** ![shiro1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shiro1/32/153306_2.png) [@shiro1](https://forum.freecodecamp.org/u/shiro1)\
**Post date:** [March 19, 2021, 7:21pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/9 "2021-03-19T19:21:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [March 19, 2021, 7:25pm UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/10 "2021-03-19T19:25:26Z")

</div>

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

```auto
body: hl7_message

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [September 18, 2021, 7:26am UTC](https://forum.freecodecamp.org/t/post-request-empty-body-parser-extended-true-still-not-working/451962/11 "2021-09-18T07:26:06Z")

</div>

Closed on September 18, 2021, 7:26am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
