I am calling prometheus server via Grafana I am able to make below request using postman but when I am trying same uri with java code getting below exception
Caused by: org.apache.http.ProtocolException: Target host is not specified
at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:71)
at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:125)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
... 31 common frames omitted
My piece of code is as given below.
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String PM_UI_SERVER_URI = "http://" + PM_SERVER_HOST + ":" + getPMUiServerPort();
String uriStr= PM_UI_SERVER_URI + PM_SERVER_BASE_URI + queryString +"&start="+String.valueOf(startTime)+"&end="+String.valueOf(endTime)+"&step=150";
//logger.info("Execute Query uri string: "+uriStr);
String str="http://10.61.244.58:31000/api/datasources/proxy/1/api/v1/query_range?query=em_core_used_heap_mem_mb{job=\"eric-em-om-server\"}&start=1592981880&end=1592982180&step=15";
String encodedurl = URLEncoder.encode(str,"UTF-8");
//URI uri = new URI(encodedurl);
//HttpGet httpget = new HttpGet("http://10.61.244.58:31000/api/datasources/1");
HttpGet httpget = new HttpGet(encodedurl);
httpget.addHeader("Authorization", token);
httpget.addHeader("Content-Type", "application/json");
CloseableHttpResponse response = httpClient.execute(httpget);
Can someone please help as I am stucked here.
Try to build up the URL piece by piece rather than going straight for the completed URL. You can do this in a debug session using the expression builder in intelliJ IDE.
set breakpoint at the line ... = httpClient.execute(httpget); and exercise this code from a test/running the application in debug mode.
highlight httpClient.execute(httpget)
either right click this selection and click "Expression builder" OR use Alt+F8
now try to perform the execute the GET (HttpGet) request for http://10.61.244.58:31000/
ensure you're getting a 200 response status or equivalent.
next you should be able to add the next bit like http://10.61.244.58:31000/api/datasources/proxy/1/api/v1/query_range (Note I'd also try without the proxy as the URL may be problematic because your requests are being proxied according to the docs here: https://grafana.com/docs/grafana/latest/http_api/data_source/#data-source-proxy-calls
I'd try then adding the query params individually and combined. This isn't going to necessarily resolve your problem... but if you can at the same time tail any logs on the grafana server/proxy server you may get some more detailed information that will help lead your investigation.
Related
i am using the CSRF Jenkins crumbs in the API call to create a new job in Jenkins from Java.
I tried the following
Called the API to get the crumb data
http://admin:11542c80972c3a2b863453d234de68b1d#10.139.163.33/crumbIssuer/api/json
I also tried with the below URL
http://10.139.163.33/crumbIssuer/api/json
The below is the JSON response obtained from the server
{"_class":"hudson.security.csrf.DefaultCrumbIssuer","crumb":"b272a09b604e7b7cc8ee1431f0a0143fa1422db2fb5f92955b0356a31da37463","crumbRequestField":"Jenkins-Crumb"}
In the next step, I am making a call to the Jenkins to create a new job with the header as
Jenkins-Crumb:b272a09b604e7b7cc8ee1431f0a0143fa1422db2fb5f92955b0356a31da37463
Jenkins is giving me 403, I am using HttpGet to get the token and using HttpPost with the header as above and sending to jenkins.
When i try with postman, it is not giving this error. I am running the Java application in 1 ec2 server and jenkins on another ec2 server.
There are no proxies, I also tried to use the various options like the enable proxy compatibilty, restarting jenkins etc, but not working.
Please give any pointers.
Java code used is
HttpPost postRequest = new HttpPost(url);
JenkinsCrumb crumb = jenkinsHelper.getCrumb();
String encodedPassword = Base64.getEncoder().encodeToString((user + ":" + pwd).getBytes());
postRequest.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedPassword);
postRequest.addHeader(new BasicHeader(crumb.getCrumbRequestField(), crumb.getCrumb()));
return postRequest;
The code to get the crumb is
String urlWithToken = "http://" + (user + ":" + pwd) + "#";
HttpGet request = new HttpGet(jenkinsBaseUrl.replace("http://", urlWithToken) + "crumbIssuer/api/json");
request.addHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedPassword);
CloseableHttpResponse httpResponse = httpClient.execute(request);
I have also tried with the CURL command and still getting the same response
I was able to fix this issue by using the same HttpClient (CloseableHttpClient) for both the CRUMB and the POST Request. Earlier, I was using 2 separate clients one for getting the crumb and new one for the posting of the data. Using a shared httpclient for both of these resulted in success state.
Hope this helps any other developer facing similar issue.
I think I am doing everything right according to the documentations I have, but I still get an error 406 with the message "Not Acceptable" when trying to check-out a test.
The steps I do are:
authenticate OK
read items and find the id of the one I want to update OK
lock my item OK
check-out --> 406
I am checking out using the following URL:
http://myHost/qcbin/rest/domains/DEFAULT/projects/myProject/tests/4313/versions/check-out
I am not sending a versions XML since the documentation says that it's not needed when checking out the last version.
Here is my code:
CloseableHttpClient client = HttpClients.createDefault();
String url = Properties.get("hpqc.base_url") + DOMAINS + Properties.get("hpqc.domain") + PROJECTS
+ Properties.get("hpqc.project") + TEST_PUT + id + VERSIONS + reservationType;
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept:", "application/xml");
CloseableHttpResponse response = client.execute(httpPost, context);
printHttpStatus(response);
printResponse(response);
alternatively I also have tried to add a version XML by adding these lines:
httpPost.setHeader("Content-Type:", "application/xml");
httpPost.setEntity(new StringEntity(versionXml, "utf-8"));
Thanks in advance for any help.
The setHeader methods used in the above piece of code contain colons (as in "Content-Type :"). The colons are redundant.
Try removing both colons in the setHeader methods and run again.
I'm using Query Tasks method: https://asana.com/developers/api-reference/tasks#query using following code snipet:
String url = API_BASE+"/tasks?completed_since=now";
System.out.println(url);
HttpGet httpget = new HttpGet(url);
httpget.addHeader( BasicScheme.authenticate(creds, "US-ASCII", false) );
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
ERROR:
https://app.asana.com/api/1.0/tasks?completed_since=now
null
org.apache.http.client.HttpResponseException: Bad Request
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:735)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:709)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:700)
I work at Asana.
Yes, the underlying message from the server is:
"Must specify exactly one of project, tag, or assignee + workspace"
We'll take a look at updating the documentation for this since it does appear to be explicit with regards to this.
I highly recommend using url as indicated in the examples.
Also, we have a Java client library that you may find useful: https://github.com/Asana/java-asana
Thanks for bringing up the documentation issue.
It looks like project is required parameter missing in documentation.
I am trying to fetch all job names by using below code
HttpGet httpGet = new HttpGet("http://myjenkins/api/json?depth=1&tree=jobs[name,jobs[name]]")
try(CloseableHttpClient httpclient = HttpClients.createDefault()) {
try(CloseableHttpResponse response = httpclient.execute(httpGet)){
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);
System.out.println(json)
}
}
}
The above code is not returning any json response (just an empty array []), but if I remove tree query in the url (http://myjenkins/api/json?depth=1), then I get json response with all jobs.
Why the query with filter is not returning any results ?. Is something wrong with HttpClient or jenkins api.
Can someone help me to resolve this issue.
Thanks
I would suggest just trying out that url in a browser. I tried it against my instance of jenkins, and it worked fine.
Also, the second parameter in the tree query seems unnecessary - even this url returns the job names -
http://myjenkins/api/json?depth=1&tree=jobs[name]
Couple of things ..
1) I tried it my browser and all queries worked well but not through java code. The reason is, in browser am already signed in (git oauth) and all queries are working where as in java am getting empty array since jenkins authorization set to not read jobs for anonymus (Stupid of me not check this before).
2) Once proper permissions are set I still had an issue with URI encoding, then I used URI builder
URI uri = new URIBuilder().setScheme("http").setHost(jenkinsHost)
.setPath("/api/json")
.setParameter("depth", "1").setParameter("tree", "jobs[name,jobs[name]]")
.build()
everything works now.
I have a link of a servlet as follow :
http://localhost:8080/UI/FacebookAuth?code=1
and I wrote a little program to connect this link, if you manually type this link in browser it types something in a console but as soon as I run my code nothing happens, it seems that the link is not executed
System.out.println("Starting...");
URI url = new URI("http://localhost:8080/UI/FacebookAuth?code=1");
HttpGet hg = new HttpGet();
hg.setURI(url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(hg);
System.out.println("Finished...");
Can anyone tell me what the problem?
Your code snippet does nothing with the response. All you do is print out, "Finished..." Because you threw away the response, you have no way of knowing what happened. Assuming that you're using the Apache HTTP client, you should add something like this:
System.out.println("Status code: " + response.getStatusLine().getStatusCode());
See http://hc.apache.org/httpcomponents-core-4.2.x/httpcore/apidocs/org/apache/http/HttpResponse.html for the methods you can execute on the response.