HAPIFhir Client and API-key

147 views
Skip to first unread message

Jozef Aerts

unread,
Aug 16, 2021, 10:40:34 AM8/16/21
to HAPI FHIR

I presume this question is a bit similar to the one at: https://groups.google.com/g/hapi-fhir/c/yhAv-VbtRZc/m/RSrRj8atAQAJ
Some (test) servers such as SynthethisMass (syntheticmass.mitre.org) require an API-key to be passed. This is easy to do when using classic RESTful-WS (e.g. using Jersey-2), but I haven't been able yet how to do this with HAPI-FHIR.
I looked into the methods for FhirContext and IGenericClient, but haven't found anything yet. I couldn't find any example either by googling.
Is there a way to pass parameters such as an API-key using HAPI-FHIR client?

Many thanks in advance,
Jozef
Message has been deleted

Juan Alberto Acebes

unread,
Sep 21, 2021, 9:27:25 AM9/21/21
to HAPI FHIR
Take a look under  ca.uhn.fhir.rest.client.interceptor.... AdditionalRequestHeadersInterceptor?

Jozef Aerts

unread,
Sep 21, 2021, 10:00:54 AM9/21/21
to HAPI FHIR
Thanks a lot! Will try it!

Jozef Aerts

unread,
Oct 14, 2021, 10:26:36 AM10/14/21
to HAPI FHIR
What is send should look like: https://syntheticmass.mitre.org/v1/fhir/Condition?code=http://snomed.info/sct|44054006&apikey=<APIKEY>
So I tried the following:

hapiFhirClient = ctx.newRestfulGenericClient(base);
            if(base.equals("https://syntheticmass.mitre.org/v1/fhir")) {
                AdditionalRequestHeadersInterceptor interceptor = new AdditionalRequestHeadersInterceptor();
                logger.debug("Using AdditionalRequestHeadersInterceptor with apikey = " + syntheaKey);
                interceptor.addHeaderValue("apikey", syntheaKey);
                hapiFhirClient.registerInterceptor(interceptor);
            } 
            Bundle bundle = null;
            try {
                IQuery query = (IQuery)hapiFhirClient.search().forResource(Condition.class)
                        .where(new TokenClientParam("code").exactly().systemAndCode("http://snomed.info/sct", "44054006") );
                Object obj = query.execute();
                logger.debug("obj = " + obj);
                if(obj instanceof Bundle) bundle = (Bundle)obj;
            } catch (Exception e) {
                System.out.println("Error - message = " + e.getMessage());
                System.out.println("Error - cause = " + e.getCause());
            }

which results in an exception error:
"Response contains no Content-Type"'

With other FHIR servers that do not require an API key to be added to the request, it works wonderful.

So I wonder: for IQuery, is there a way to see how the query string (as URL) looks like? So that I can see what is exactly send to the server.

Any other suggestions are of course welcome!

Many thanks in advance,
Jozef
Reply all
Reply to author
Forward
0 new messages