Search code examples
rest-client

REST Client: Error was: HAPI-1861: Failed to parse JSON encoded FHIR content: Unexpected character ('@' (code 64)): expected a valid value


I am trying to understand FHIR fundamentals and I have installed REST Client in VS code to post the following

### Variable declaration ###
@host = https://hapi.fhir.org/baseR4
@contentType = application/json

### Post Patient ###
# @name createPatient
POST {{host}}/Patient HTTP/1.1
content-type: {{contentType}}

{
    "resourceType": "Patient",
    "name": [
        {
            "use" : "official", 
            "family" : "Vijay", 
            "given" : ["Karthik"]
      }
    ],
    "gender": "male",
    "birthDate": "1998-02-26",
    "telecom":[
        {
            "value": "9000000000",
            "use": "mobile",
            "system": "phone"
        },
        {
            "value": "[email protected]",
            "system": "email"
        }        
    ],
    "address": [
        {
            "line": [
                "213, Diamond Residency"
            ],
            "city": "Manipal",
            "state": "Karnataka",
            "postalCode": "567104"
        }
    ]
}

and it works without any issues

{
  "resourceType": "Patient",
  "id": "10690049",
  "meta": {
    "versionId": "1",
    "lastUpdated": "2023-06-05T16:56:48.315+00:00",
    "source": "#JXAcEJnruLAVvERP"
  },
  "text": {
    "status": "generated",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><div class=\"hapiHeaderText\">Karthik <b>VIJAY </b></div><table class=\"hapiPropertyTable\"><tbody><tr><td>Address</td><td><span>213, Diamond Residency </span><br/><span>Manipal </span><span>Karnataka </span></td></tr><tr><td>Date of birth</td><td><span>26 February 1998</span></td></tr></tbody></table></div>"
  },
  "name": [
    {
      "use": "official",
      "family": "Vijay",
      "given": [
        "Karthik"
      ]
    }
  ],
  "telecom": [
    {
      "system": "phone",
      "value": "9000000000",
      "use": "mobile"
    },
    {
      "system": "email",
      "value": "[email protected]"
    }
  ],
  "gender": "male",
  "birthDate": "1998-02-26",
  "address": [
    {
      "line": [
        "213, Diamond Residency"
      ],
      "city": "Manipal",
      "state": "Karnataka",
      "postalCode": "567104"
    }
  ]
}

as you see above it returns the valid JSON with the following headers

HTTP/1.1 201 Created
Server: nginx/1.18.0 (Ubuntu)
Date: Mon, 05 Jun 2023 21:53:13 GMT
Content-Type: application/fhir+json;charset=utf-8
Transfer-Encoding: chunked
Connection: close
X-Powered-By: HAPI FHIR 6.7.3-SNAPSHOT/d94627c382/2023-05-18 REST Server (FHIR Server; FHIR 4.0.1/R4)
ETag: W/"1"
X-Request-ID: ReW0SjpvyU7OG0SN
Content-Location: https://hapi.fhir.org/baseR4/Patient/10691862/_history/1
Last-Modified: Mon, 05 Jun 2023 21:53:13 GMT
Location: https://hapi.fhir.org/baseR4/Patient/10691862/_history/1
Content-Encoding: gzip

However, while trying to set the variable like mentioned below

### Variable declaration ###
@host = https://hapi.fhir.org/baseR4
@contentType = application/json

### Post Patient ###
# @name createPatient
POST {{host}}/Patient HTTP/1.1
content-type: {{contentType}}

{
    "resourceType": "Patient",
    "name": [
        {
            "use" : "official", 
            "family" : "Vijay", 
            "given" : ["Karthik"]
      }
    ],
    "gender": "male",
    "birthDate": "1998-02-26",
    "telecom":[
        {
            "value": "9000000000",
            "use": "mobile",
            "system": "phone"
        },
        {
            "value": "[email protected]",
            "system": "email"
        }        
    ],
    "address": [
        {
            "line": [
                "213, Diamond Residency"
            ],
            "city": "Manipal",
            "state": "Karnataka",
            "postalCode": "567104"
        }
    ]
}

@patientId = {{createPatient.response.body.$.id}

it throws the following error

{
  "resourceType": "OperationOutcome",
  "text": {
    "status": "generated",
    "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><h1>Operation Outcome</h1><table border=\"0\"><tr><td style=\"font-weight: bold;\">ERROR</td><td>[]</td><td>HAPI-0450: Failed to parse request body as JSON resource. Error was: HAPI-1861: Failed to parse JSON encoded FHIR content: Unexpected character ('@' (code 64)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: UNKNOWN; line: 35, column: 2]</td></tr></table></div>"
  },
  "issue": [
    {
      "severity": "error",
      "code": "processing",
      "diagnostics": "HAPI-0450: Failed to parse request body as JSON resource. Error was: HAPI-1861: Failed to parse JSON encoded FHIR content: Unexpected character ('@' (code 64)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: UNKNOWN; line: 35, column: 2]"
    }
  ]
}

Solution

  • The error is returned by HTTP server and is about line:

    @patientId = {{createPatient.response.body.$.id}
    

    This happened because REST Client treated it as part of the body and sent it literally to the server. Thus it complained about '@' not being correct JSON.

    To fix it make variable assignment part of a separate request. Place ### as request separator after the body. Like this:

    ### Post Patient ###
    # @name createPatient
    POST {{host}}/Patient HTTP/1.1
    content-type: {{contentType}}
    
    {
      [...]
    }
    
    ###
    
    @patientId = {{createPatient.response.body.$.id}}
    
    GET {{host}}/Patient/{{patientId}} HTTP/1.1
    

    Above yields correct result:

    {
      "resourceType": "Patient",
      "id": "10741308",
      [...]
    }