Sam
Sam

Reputation: 548

Post to Azure FHIR Server method Fail to Authentication Failed 401

I have an Azure FHIR Server which I have tested with POSTMAN I am able to Read Patients as well as POST Patients using POSTMAN and Oauth token.

I have now created a JavaScript Post method so that I can post data from my own method rather than using POSTMAN. I have followed the basic guidance from here Creating a resource I can get an access token using POSTMAN and I use that access token in my POST method.

My method and post data is as follow.

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript">

  (async () => {
  const rawResponse = await fetch('https://smarthealth.azurehealthcareapis.com/Patient', {
  method: 'POST',
  Authorization: 'Bearer DGRijc8LrnjCrhRFRSn2Jnf89q2wGT_gxNHtJt',
   headers: {
              'Access-Control-Allow-Origin': '*',
               'Accept': 'application/json',
               'Content-Type': 'application/json',
               'Access-Control-Allow-Origin': 'http://localhost:3000',
               'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS'
                },
    body: JSON.stringify({
    "resourceType": "Patient",
    "id": "9184e31a-a2b3-4b8d-9032-d5377cabaa08",

       "extension": [
         {
          "url": "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race",
            "extension": [
               {
                "url": "ombCategory",
              "valueCoding": {
               "system": "urn:oid:2.16.840.1.113883.6.238",
             "code": "2106-3",
             "display": "White"
                }
                  },
           {
             "url": "text",
           "valueString": "White"
                         }
                            ]
                        },
                        {
              "url": "http://hl7.org/fhir/StructureDefinition/patient-birthPlace",
        "valueAddress": {
         "city": "Mashpee",
          "state": "Massachusetts",
          "country": "US"
                            }
                        },

                    ],

                    "name": [
                        {
                            "use": "official",
                            "family": "Kutch",
                            "given": [
                                "Sherron"
                            ]
                        }
                    ],

                    "gender": "female",
                    "birthDate": "2009-04-29",

                })
            });
            const content = await rawResponse.json();

            console.log(content);
        })();

    </script>
</head>

<body>
    Javascript Azure FHIR Post Request Test
</body>

</html>

Authentication Failed Image here

Upvotes: 1

Views: 515

Answers (1)

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22495

You could try sending your request according to below format.

  $("#btnPostToAzureFHIR").click(function () {
        var yourPostDataFormat = {
            resourceType: "Patient",
            id: "9184e31a-a2b3-4b8d-9032-d5377cabaa08"
        }
        $.ajax({
            type: "POST",
            url: "https://smarthealth.azurehealthcareapis.com/Patient",
            data: JSON.stringify(yourPostDataFormat),
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization','Bearer DGRijc8LrnjCrhRFRSn2Jnf89q2wGT_gxNHtJt');
            },
            dataType: "json",
            contentType: "application/json",
            success: function (data) {
                console.log(data);

            }
        });

Note: Bind your parameter in key value pair on yourPostDataFormat then pass on JSON.stringify(yourPostDataFormat) section. Hope it would post your request with token

Upvotes: 2

Related Questions