How to write a HTTP response in PHP on a POST request from a HAPI-FHIR client

72 Views Asked by At

The main problem is that the HTTP response which I've sent towards the client causes an exception at the client's side: "Failed delivery for (MessageId: ID: on ExchangeId: <..>). caught: ca.uhn.fhir.rest.client.exceptions.FhirClientConnectionException: HAPI-1359: Failed to parse response from server when performing POST to URL ". And a DataFormatException: "HAPI-1861: Failed to parse JSON encoded FHIR content: HAPI-1857: Did not find any content to parse" as well. Because of this, the client does endless retries by resending the same message (Bundles) every hour or so.

What I have programmed in my PHP application, is:

            $_http_status_line = "HTTP/1.1 201 Created "; 
    $_date_string = make_date_string_reponse($_debug_level); 
            $_general_header = $_date_string . "; ";  
            $_etag_header = "ETag: W/\"1\"; "; 
            $_location_header = "Location: https://<some.url>/; "; 
            $_response_header = $_etag_header . " " . $_location_header . " "; 
    $_last_modified_date_string = make_date_string_reponse($_debug_level); 
            $_content_type_header = "Content-Type: application/fhir+json;charset=utf-8; "; 
            $_last_modified_header = "Last-Modified: {$_last_modified_date_string}; "; 
            $_entity_header = $_content_type_header . $_last_modified_header . " "; 
            $_http_header = $_general_header . $_response_header . $_entity_header . "\n" ; 
            $_http_response = $_http_status_line . $_http_header;   
            header($_http_response); 

And this piece of code generates the following HTTP response:

HTTP/1.1 201 Created Tue, 12 Dec 2023 04:54:37 GMT; ETag: W/"1"; Location: https://<some.url>/; Content-Type: application/fhir+json;charset=utf-8; Last-Modified: Tue, 12 Dec 2023 04:54:37 GMT;

The client thinks now there are no headers to parse and generates an exception and, as a consequence, retries the message. What I have expected is that the headers reach the client and no retries will be done.

My question is now: is this the right way to code HTTP responses in PHP or not?

Thanks in advance. Kind regards, evert.

1

There are 1 best solutions below

0
On

HTTP headers should be formatted as key: value pairs, each terminated with a newline (\n). Your current method of concatenating headers into a single string and then using header() once to send them all may not be correctly separating the headers, which is most likely the reason client complains.