Failed to parse request body as JSON resource


#1

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?


#2

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…


#3

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'}]}

#4

Yep, thanks

Resolve, I sent wrong request body. Right

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