Failed to parse request body as JSON resource

Use fhir.js package

FhirServise.js

const mkFhir = require('fhir.js');

class FhirService {
  constructor(token) {
    this.client = mkFhir({
      baseUrl: process.env.FHIR_URI,
      auth: {
        bearer: token,
      },
    });
  }
  async updatePatient(resource) {
    const response = await this.client.update(resource);

    return response
  }
}

module.exports = FhirService;

Next use updatePatient

const router = require('express').Router();
const { celebrate } = require('celebrate');
const validators = require('../../validators/patient');
const FhirService = require('../../services/fhir');

router
  .route('/')
  .put(celebrate(validators.putPatient), async (req, res, next) => {
    try {
      const token = req.headers['x-access-token'];
      const fhirClient = new NewFhirService(token)
      
      const response = await fhirClient.updatePatient(req.body);

      return res.send(response);
    } catch (err) {
      next(err);
    }
  })
  

module.exports = router;

req.body has a structure

{type: "Patient", id: "17865", reporting: {email: true}}

And get error

error: ā€œOperation Outcomeā€
message: ā€œFailed to parse request body as JSON resource. Error was: Failed to parse JSON content, error was: Did not find any content to parseā€
statusCode: 400

If try to use JSON.strigify()

const router = require('express').Router();
const { celebrate } = require('celebrate');
const validators = require('../../validators/patient');
const FhirService = require('../../services/fhir');

router
  .route('/')
  .put(celebrate(validators.putPatient), async (req, res, next) => {
    try {
      const token = req.headers['x-access-token'];
      const fhirClient = new NewFhirService(token)
      
      const response = await fhirClient.updatePatient(JSON.stringify(req.body));

      return res.send(response);
    } catch (err) {
      next(err);
    }
  })
  

module.exports = router;

Get error

{statusCode: 400, error: ā€œI need adapter.deferā€, message: ā€œI need adapter.deferā€}

Where am I wrong?

Patient doesn’t have an element called ā€œreportingā€. Your JSON is invalid. That said, the error message is certainly unclear if that’s the only problem…

1 Like

It looks like the Patient record in your body may not be valid. You need to use a resourceType instead of ā€˜type’, and reporting is a non-standard field. Try something like the following:

{resourceType: "Patient", id: "17865", name: [{ text: 'Jane Doe'}]}
1 Like

Yep, thanks

Resolve, I sent wrong request body. Right

({
  resource: {
    resourceType: type,
    id,
    ...rest
  }
})