From d268fd6e1e09d5e990e7ab6a08bf7f0723c43f32 Mon Sep 17 00:00:00 2001 From: fikrimilano Date: Mon, 26 Aug 2024 17:11:26 +0700 Subject: [PATCH] Update local resource with id from server --- uploader/uploader.sh | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) mode change 100644 => 100755 uploader/uploader.sh diff --git a/uploader/uploader.sh b/uploader/uploader.sh old mode 100644 new mode 100755 index 788b481..d1b5048 --- a/uploader/uploader.sh +++ b/uploader/uploader.sh @@ -5,11 +5,30 @@ push_to_server() { RESOURCE_TYPE=$(cat "$@" | jq -r '.resourceType') # Post to server - SERVER_URL="$SERVER_URL/${RESOURCE_TYPE}" + ENDPOINT_URL="${SERVER_URL}/${RESOURCE_TYPE}" echo -e '\n' echo Resource File: "$@" - echo Endpoint: "$SERVER_URL" - curl --write-out "%{http_code}\n" -X POST $SERVER_URL -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/fhir+json" -d @"$@" --silent >> output.txt + echo Endpoint: "$ENDPOINT_URL" + + # Make the POST request + RESPONSE=$(curl -i -X POST "$ENDPOINT_URL" -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/fhir+json" -d @"$@" --silent) + echo "$RESPONSE" >> response.txt + + # Extract the location header + LOCATION_HEADER=$(echo "$RESPONSE" | awk '/^location:/ { print $0 }') + echo "Location Header: $LOCATION_HEADER" + + # Extract the generated resource ID + GENERATED_ID=$(echo "$LOCATION_HEADER" | sed -n 's|.*/\([^/]*\)/_history.*|\1|p') + echo "Extracted Generated ID: $GENERATED_ID" + + if [ ! -z "$GENERATED_ID" ]; then + # Update the resource ID in the file + jq --arg id "$GENERATED_ID" '.id = $id' "$@" > tmp.$$.json && mv tmp.$$.json "$@" + echo "Updated the resource ID in $@ to $GENERATED_ID." + else + echo "Failed to obtain the generated ID from the server." + fi } main() {