karatelabs / karate

Test Automation Made Simple
https://karatelabs.github.io/karate
MIT License
8.31k stars 1.95k forks source link

Request body empty when use Content-Type x-application/hl7-v2+er7 #2626

Open andreas-hempel opened 1 week ago

andreas-hempel commented 1 week ago

Hello KarateDSL team,

I am using KarateDSL to check if an HL7 message processing is executed correctly and the data is rendered correctly via a second FHIR endpoint (REST based). When sending the message, the content-type x-application/hl7-v2+er7 must be used (see https://hapifhir.github.io/hapi-hl7v2/hapi-hl7overhttp/specification.html#a2.4_Content_Type_and_Character_Set)

If I set the content type to x-application/hl7-v2+er7; charset=utf-8 for a POST request, the request body is empty. In the appendix you will find an example project to simulate the behavior.

missing-body.zip

In the example there are two scenarios, one with content-type x-application/hl7-v2+er7; charset=utf-8 and one with content-type application/plain; charset=utf-8. The request body is in both scenrios the same.

* url 'https://httpbin.org'
* def message = "MSH|^~\&|ADT1|GOOD HEALTH HOSPITAL|GHH LAB, INC.|GOOD HEALTH HOSPITAL|198808181126|SECURITY|ADT^A01^ADT_A01|MSG00001|P|2.8||"

Given path 'post'
And header Content-Type = 'x-application/hl7-v2+er7; charset=utf-8'
And request message
When method post
Then status 200

log entry with missing body (Content-Type: x-application/hl7-v2+er7; charset=utf-8)

15:24:25.410 [pool-2-thread-1] DEBUG com.intuit.karate - request:
1 > POST https://httpbin.org/post
1 > Content-Type: x-application/hl7-v2+er7; charset=utf-8
1 > Content-Length: 123
1 > Host: httpbin.org
1 > Connection: Keep-Alive
1 > User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.8.1)
1 > Accept-Encoding: gzip,deflate

log entry with body (Content-Type: text/plain; charset=utf-8)

15:24:25.410 [pool-2-thread-2] DEBUG com.intuit.karate - request:
1 > POST https://httpbin.org/post
1 > Content-Type: text/plain; charset=utf-8
1 > Content-Length: 123
1 > Host: httpbin.org
1 > Connection: Keep-Alive
1 > User-Agent: Apache-HttpClient/4.5.14 (Java/17.0.8.1)
1 > Accept-Encoding: gzip,deflate
MSH|^~&|ADT1|GOOD HEALTH HOSPITAL|GHH LAB, INC.|GOOD HEALTH HOSPITAL|198808181126|SECURITY|ADT^A01^ADT_A01|MSG00001|P|2.8||

I've tested everything in a new minimal maven karate template as explained in https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue, and you can replicate everything in the code in the attached .zip using the command: mvn clean test

Many thanks and best regards, Andreas

ptrthomas commented 1 week ago

@andreas-hempel appreciate the details and the info to replicate. actually, when logging the request, karate does not attempt to log the body if it is some "unknown" type, and the reason for this is to avoid logging binary content. so that explains that part

so look at the response from httpbin.org you will see that the request string was indeed sent and received successfully on the server:

image

we could probably introduce a new configuration setting to over-ride the request (or response) body logging suppression, so I can leave this issue open for now.

just one tip - in case you have weird content that confuses the JSON parser or spans multiple lines, you can consider using text. it won't solve your original problem, but may be useful:

    * text body =
    """
    MSH|^~\&|ADT1|GOOD HEALTH HOSPITAL|GHH LAB, INC.|GOOD HEALTH HOSPITAL|198808181126|SECURITY|ADT^A01^ADT_A01|MSG00001|P|2.8||
    """
    * url 'https://httpbin.org/post'
    * header Content-Type = 'x-application/hl7-v2+er7'
    * request body
    * method post

can you confirm that the HTTP call works fine at least from the server pov ? if your intent is to have the body show up in the HTML report somehow, you could always do * print body

andreas-hempel commented 1 week ago

@ptrthomas thank you very much for the quick feedback. I have now been able to verify on the server that the request body was transmitted.