node.jsexpresshl7-fhir

FHIR Server: Receiving 'Undefined' Request Body in POST Request Despite Correctly Formatted JSON


Problem Description: I'm building a FHIR server using Node.js and Express, and I'm trying to create a new Patient resource by sending a POST request. However, I've been encountering issues where the server receives undefined for the request body, despite using different REST Clients (Thunder Client and VS Code REST Client) to send the request.

Steps Taken:

Checked Request Body Format: I verified that the JSON request body is correctly formatted with the required fields, including "resourceType", "name", "gender", and "birthDate".

Client Configuration: I ensured that the headers in both REST Clients, including "Content-Type" and "Accept", are correctly set to "application/fhir+json" with the appropriate fhirVersion.

Request Method: I confirmed that I'm using the POST method for the request.

Endpoint URL: I verified that the endpoint URL is set to http://localhost:3000/4_0_0/Patient to match the server route.

Content-Length Header: The content-length header appears to be automatically set by the client, and I didn't manually modify it.

REST Client Settings: I checked for any specific settings or options in both REST Clients that might be affecting how the request body is sent. I didn't identify any settings that seemed to be causing the issue.

Restarted the Server: I tried restarting the server in case changes to the server code needed to take effect.

Tried Different Clients: I attempted to send the request using both Thunder Client and VS Code REST Client to see if the issue was specific to one client, but the problem persisted in both.

Checked for Errors: I looked for error messages or warnings in the client console or logs but didn't find any that provided insights into the problem.

Despite these efforts, I continue to face the issue where the server receives undefined for the request body when trying to create a Patient resource.

const { VERSIONS } = constants;
const express = require('express');

let config = {
  profiles: {
    patient: {
      service: './patient.service.js',
      versions: [VERSIONS['4_0_0']],
    },
  },
};

let server = initialize(config);
let logger = loggers.get('default');

// Create an Express application
const app = express();

// In-memory data store for Patient resources
const patientDataStore = [];

// creating a Patient resource
app.post('/4_0_0/Patient', (req, res) => {
    console.log('Received POST request with data:', req.body);
    console.log('Request headers:', req.headers);
  const patientData = req.body;

  console.log('Received POST request with data:', patientData);

  // Perform validation, data storage, etc.
  // For simplicity, we'll just push the patientData to the in-memory store
  patientDataStore.push(patientData);
  console.log('Patient data stored:', patientDataStore);
  // Send a response indicating success (HTTP status code 201 for resource creation)
  res.status(201).json({ message: 'Patient resource created successfully' });
});

// Define route handlers for GET requests to the URLs
app.get('/4_0_0/', (req, res) => {
   
    res.send('GET request to /4_0_0/');
  });
  
  app.get('/4_0_0/Patient', (req, res) => {
    console.log('Data in patientDataStore:', patientDataStore);
    res.json(patientDataStore);
});

// Define a route to retrieve and display patient data
app.get('/patientData', (req, res) => {
    console.log('GET request to /patientData');
    console.log('Data in patientDataStore:', patientDataStore);
    res.json(patientDataStore);
  });
  

const fhirMiddleware = (req, res, next) => {

  server(req, res, next);
};

app.use('/fhir', fhirMiddleware);

const PORT = 3000;

app.listen(PORT, () => {
  logger.info(`Starting the FHIR Server at localhost:${PORT}`);
});```

Solution

  • Adding my comment as a answer, so this question can be closed out.

    "application/fhir+json"

    can you try (on the client) just "application/json" ?

    I'm not sure the fhir+json is "understood".

    Have you tried just posting a simple json

    { "hello": "world" } 
    

    to your new rest service?

    If this helped, then please mark "as the answer" so this question does not come up on "unanswered questions".

    Also, please append your original question with what you discovered as the issue/answer..so people in the future can also learn from this question/answer.