GET /tasks does not work - java

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.

Related

How to make Java HttpPost NOT include url in body

We have some old java code that POSTs some fields and values to a dotnet5 web api - The api is having problems dealing with the body of the POST as it includes the url/uri as the first part of the body.
The Java sends: http://127.0.0.1:5555?producerRef=GREEN&systemId=78&status=false
But the api is expecting something like: producerRef=GREEN&systemId=78&status=false
as per https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#example. If we send a test message via Postman then the api has no problems.
This is the Java code:
List<NameValuePair> params = new ArrayList<NameValuePair>(queryParams.size());
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
// the address is just that, there's NO parameters
HttpPost post = new HttpPost(this.cmAddress.toURI());
post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
post.setHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
CloseableHttpResponse response = httpClient.execute(post);
It's quite simple, but always adds the url to the start of the body of the request. If this is the only way to produce this, what could I do to produce something that looks like this: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#example
Many Thanks.
This request seems like a GET request rather than a POST since the request params are in the URL. i don't know about the specifications of the Api you're using, but you can try OKHTTP, you can easily copy the code directly from postman
Postman Get example:
Your issue seems to be at below line
HttpPost post = new HttpPost(this.cmAddress.toURI());
This is the only place which will set the POST url ( another way is to use setURI which is not called anywhere in the code sample you have shared).
If you can use a debugger try checking the value of cmAdress variable

Getting Target host is not specified

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.

Post with HttpClient returns Bad Request

I am using Java, Spring boot and Apache HttpClient to try send a post request. The documentation of the resource I am trying to reach can be found here:
https://docs.enotasgw.com.br/v2/reference#incluiralterar-empresa
Below is my code:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(incluirEmpresa);
post.setHeader("Content-Type", "application/json");
post.setHeader("Accept", "application/json");
post.setHeader("Authorization", "Basic " + apiKey);
try {
StringEntity entity = new StringEntity(json);
//tried to add these two lines to see if they would fix the error, but it is the same
entity.setContentEncoding("application/json");
entity.setContentType("application/json");
post.setEntity(entity);
System.out.println(json);
System.out.println("======================");
CloseableHttpResponse response = httpClient.execute(post);
System.out.println(response.getStatusLine().getReasonPhrase() + " - " + response.getStatusLine().getReasonPhrase());
idEmpresa = response.getEntity().getContent().toString();
}
My response is 400 - Bad Request. On the interactive documentation link above, when I post my Json, I receive the error of duplicate entry, which is what I expect since the information I am sending is already on the database.
Since the interactive documentation returns the error of duplicate, I know the problem is not within my json format, but on my post request. The documentation have samples on C#, but not on Java, which is what I am using.
By the way, the json is variable is a string in case this is relevant.
Could someone try to point to me what is wrong with my post code?
Found out what I was missing.
After reviewing what was being sent to the API, i noticed the json was not in the expected format. So I did some research and found that, at least for my case, setting the headers with the content type was not enough, I also had to set the Entity that was being set to the HttpPost, to do that, i had to change this line of the code:
StringEntity entity = new StringEntity(json);
to this:
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
After that change, the requests started to work as expected.

Jenkins API : issues with tree filter in java

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.

how to connect to url from java

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.

Categories

Resources