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

Java rest service for user sessions help

abhilash_mittap
Newcomer

Below is the Java class which i have written to call a usersession api , but i get 400 as response. Any help here could be appreciated. Is it issue with setting api header ? The same url is working in post man client.


package com.spx.dyntrace;


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.util.Base64;

public class Post {





// http://localhost:8080/RESTfulExample/json/product/get

public static void main(String[] args) {


try {

String API_KEY= "YOUR KEY";


URL url = new URL("https://YYYYYY.live.dynatrace.com/api/v1/userSessionQueryLanguage/table?query=SELECT endTime as time, userSessionId , SUM((useraction.visuallyCompleteTime)) AS 'Server Processing Time'FROM usersession WHERE (userId='Full Quote User 1' OR internalUserId='Full Quote User' or userId='Full Quote User 2' or userId='Full Quote User 3' or userId=\"Full Quote User\") and userActionCount > 25 and userActionCount <50 GROUP BY endTime ,userSessionId&startTimestamp=1586905282288");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setRequestProperty("Accept", "application/json");

//conn.addRequestProperty("Authorization", basicAuth);

conn.setRequestProperty("Authorization", "Api-Token=YYYYYYYY");

conn.setDoInput(true);



if (conn.getResponseCode() != 200) {

throw new RuntimeException("Failed : HTTP error code : "

+ conn.getResponseCode());

}


BufferedReader br = new BufferedReader(new InputStreamReader(

(conn.getInputStream())));


String output;

System.out.println("Output from Server .... \n");

while ((output = br.readLine()) != null) {

System.out.println(output);

}


conn.disconnect();


} catch (MalformedURLException e) {


e.printStackTrace();


} catch (IOException e) {


e.printStackTrace();


}


}


}

}


7 REPLIES 7

Radoslaw_Szulgo
Dynatrace Guru
Dynatrace Guru

Seems like the URL is invalid. Try encoding the param:

String query = "SELECT endTime as time, userSessionId , SUM((useraction.visuallyCompleteTime)) AS 'Server Processing Time'FROM usersession WHERE (userId='Full Quote User 1' OR internalUserId='Full Quote User' or userId='Full Quote User 2' or userId='Full Quote User 3' or userId=\"Full Quote User\") and userActionCount > 25 and userActionCount <50 GROUP BY endTime ,userSessionId, tTimestamp=1586905282288"
String url = "https://YYYYYY.live.dynatrace.com/api/v1/userSessionQueryLanguage/table?query=" + URLEncoder.encode(query, "UTF-8");
Senior Product Manager,
Dynatrace Managed expert

I think issue is wth api token , any idea how do we set api token

You mentioned this was successful using Postman. Did you use the same Token for both Postman and the Java app? If so, then this assumes the API token is set correctly. The API token needs User Sessions permission.

Can you verify this token has this permission?

A second idea: In your code your API_KEY is set to a string, which I assume you replace with your real API Token. But then you never use API_KEY string, but instead insert "YYYYYY" as your token. This API token needs to be the token you get from Dynatrace GUI. Perhaps this is obvious, but then please explain your code which does not seem to use a valid token.

Hi Joseph ,

Yes i am using same API key that i am using in postman .


Is below property rightly setup ?

conn.setRequestProperty("Authorization", "Api-Token=YYYYYBLdTE5Aa");


Yes, your usage of conn.setRequestProperty() seems correct, although I've not tested your code. Did you explore Radoslaw's suggestion that the URL needs to be encoded?

Please read my comment earlier. You need to remove the "=" after Api-Token".

dave_mauney
Dynatrace Champion
Dynatrace Champion

1. Test the query in our Environment API OpenAPI page. There were some issues with it.

2. Copy the URL from the resulting curl command when you get it working

3. Change your code slightly when setting the headers:

conn.setRequestProperty("accept", "application/json");

conn.setRequestProperty("Authorization", "Api-Token **********************");

accept in lowercase and space rather than "=" after "Api-Token".

I got it working after making these changes and using the URLEncoded URL from the OpenAPI page with this query:

SELECT endTime as time, userSessionId, SUM(useraction.visuallyCompleteTime) AS 'Server Processing Time' FROM usersession WHERE (userId='Full Quote User 1' OR internalUserId='Full Quote User' or userId='Full Quote User 2' or userId='Full Quote User 3' or userId='Full Quote User') and userActionCount > 25 and userActionCount < 50 GROUP BY endTime, userSessionId

4. After you get it working like this, add the URLEncoder as suggested by @Radoslaw S.

Featured Posts