cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Does anyone know why my java code to call the REST API is failing when it works via a browser?

jeff_rowell
Helper

I am trying to use the DT API to programmatically retrieve
information on monitored hosts. My code is implemented in Java and it works
correctly until I try to add a parameter to return only hosts that have a
specific tag. I.e., I make a call referencing a URL like:

https://dtmanaged-lab/e/<our
env ID>/api/v1/entity/infrastructure/hosts?Api-Token=<the token>&tag=APP=MyAppTag(ABC)
– ENV=TEST

This fails when it is called via the java code, but works if I
copy/paste the URL into a browser. If, however, I remove the tag reference (as
below) it works when called by my java program.

https://dtmanaged-lab/e/<our
env ID>/api/v1/entity/infrastructure/hosts?Api-Token=<the token>

Has anyone encountered this type of problem before? Since the URL
works in a browser and works if I remove the tag reference I wonder if the
& character, or perhaps other characters that are in my tag like the ‘(‘ or
‘)’ character are somehow causing the code to fail. I initially assumed, of course, that there was something wrong with how my code was constructing the URL, but as mentioned above the URL (as shown when I run the program in debug mode) looks correct + works when I copy/paste it into a browser.

My code looks like the
following, and the failure occurs when creating the input stream reader.

myUrl = new URL(httpsURL);

conn = (HttpsURLConnection)myUrl.openConnection();

is = conn.getInputStream();

isr = new InputStreamReader(is);

br = new BufferedReader(isr);

APICallResults = br.readLine();

The IOException error I receive states “at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown
Source)”


2 REPLIES 2

rodrigo_alvare1
Dynatrace Champion
Dynatrace Champion

Hello,

I think your code is having issues while parsing the URL, try to do the same query but instead of using an complex tag like "tag=APP=MyAppTag(ABC) – ENV=TEST" create a manual one "test" and try with "tag=test".
This should work and then move forward with the rest of special characters you are using on your tags

Regards


Thanks Rodrigo... I ended up switching to use a filter on host groups and that worked perfectly. It would, indeed, look like the issue relates to some of the special characters found in our tag names.


Featured Posts