Error code 400 when call Jira REST API from Jira plugin

Hi,

In my Jira (server) plugin, I want to call call to a Jira REST API to execute a JQL statement: assignee = currentUser()

The URL for that query would be: http://localhost:2990/jira/rest/api/2/search?jql=assignee+%3D+currentUser()+
I tried it in my browser and it works.

So this is my code:

String searchURL = "http://localhost:2990/jira/rest/api/2/search?jql=assignee+%3D+currentUser%28%29+";
String searchURL = jiraBaseURL + SEARCH_URL + URLEncoder.encode(jql, "UTF-8");
URL url = new URL(searchURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
  response.append(line);
  response.append('\r');
}
rd.close();

Some how I got a 400 (Bad request) error code. It’s not 401 (Authentication failed) error code. So I don’t know what is the the problem and how to fix it.
Is it a problem of calling Jira Rest API from Jira plugin which is running in same Jira instance?

Any help would be appreciated!

Hi @hy.duc.nguyen,

There shouldn’t be a problem calling Jira Rest API from your app, however, it’s easier if you’d useJava API to interact with host product

If you’re interested, there’s a Java library you can use that abstracts Rest API:

1 Like

Hi @acalantog, thank you for you reply.

Currently I cannot find any built in API in Jira Java API for JQL executing, so I think using Rest API is my only way. I’m also trying with Jira Rest Java client but I have problem with that way also, which I had posted as another question.

I would be nice if I can find some help with current way, using HttpUrlConnection. I think everything seems so right but I still got the 400 error code. May be just some tiny thing is missing! :frowning:

Were you able to get this working? I’m getting the same 400 error when I try to call the REST API manually. …and there are bugs in the Atlassian Java client library that make it unusable for certain operations. Disappointing to say the least.