Search and POST with Reference to Search Result in Same Transaction

Making requests to a HAPI server (or ideally any “reasonable” server with FHIR R4) I want to POST an Observation (or something) to a Patient I don’t yet know the id of. I do know its identifier, though, and now want in one transaction to both use this identifier to find the Patient and POST an Observation with the search result as its subject. If the search failed (Patiens doesn’t exist), the whole transaction should fail.

Kind of like…

{
	"resourceType": "Bundle",
	"type": "transaction",
	"entry": [
		{
			"fullUrl": "urn:uuid:a6147ef2-343c-4024-a625-448f8315bb4b",
			"request": {
				"method": "POST",
				"ifNoneExist": "identifier=http://example.org/patient|10"
			},
			"resource": {
				"resourceType": "Patient",
				"identifier": [
					{
						"system": "http://example.org/patient",
						"value": "10"
					}
				]
			}
		},
		{
			"request": {
				"method": "POST"
			},
			"resource": {
				"resourceType": "Observation",
				"subject": {
					"reference": "urn:uuid:a6147ef2-343c-4024-a625-448f8315bb4b"
				},
				"status": "final"
			}			
		}
	]
}

…but with kind-of-GET instead of POST, as this example transaction could inadvertently create a Patient and/or could fail just because the user doesn’t have POST-rights for Patients. (Though, GET returns a search result bundle and not a single resource, which wouldn’t help here.)

Is this possible or do I have to do two requests (first a GET, then the transaction)?

I don’t want to use logical references in the Observation as they don’t seem to be considered when requesting $everything of a Patient.

You can’t use a GET and a POST in the same transaction and reference the results of the GET in the POST. However, you CAN place a POST in a transaction where the reference is a search instead of a URL. It’s called a conditional reference and can only be performed within a transaction. From the description of your issue, I think it’ll do the trick. I believe HAPI supports this, but you’ll need to verify.

Ah, thank you! The public HAPI Server demo seems to have some strange issue with my URI reference but the hapi-fhir-jpaserver-starter from GitHub (using v5.6.0) works exactly as you (and the webpage) say.