17 Apr 2020 04:53 PM
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();
}
}
}
}
Solved! Go to Solution.
17 Apr 2020 07:20 PM - last edited on 27 Mar 2023 11:26 AM by MaciejNeumann
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");
17 Apr 2020 10:14 PM
I think issue is wth api token , any idea how do we set api token
17 Apr 2020 11:27 PM
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.
18 Apr 2020 12:21 AM
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");
18 Apr 2020 03:17 AM
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?
22 Apr 2020 01:08 PM
Please read my comment earlier. You need to remove the "=" after Api-Token".
21 Apr 2020 07:17 PM
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.