1

I am consistently getting 404 response code when trying to query for an issue in Jira via Java/JQL/Jira API. The Urls below - when plugged in directly into the browser, i get correct results for both. Only via Java API do I get 404. I suspect something is wrong with the URI encoding but cannot figure it out.

Side note - the httpGet() works fine for every other Jira request I put together (Zapi, REST call, etcl), just not this particular one for whatever reason.

import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URL;

...

String urlStr = "https://jira.xxxx.com//jira/rest/api/2/issue/search?jql=project=XXXXX and summary ~ \"Testing Jira Integration Pass\"&fields=id,key,summary";

logger.debug("Url: " + urlStr.toString());

URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

logger.debug("Url Encoded: " + url.toString());

HttpURLConnection conn = httpGet(url.toString(), null, null);
logger.debug("Http Response Code: " + conn.getResponseCode());

Results:

2016-05-26 06:27:55 DEBUG - Url: https://jira.xxxx.com//jira/rest/api/2/issue/search?jql=project=XXXXX and summary ~ "Testing Jira Integration Pass"&fields=id,key,summary
2016-05-26 06:27:55 DEBUG - Url Encoded: https://jira.xxxx.com//jira/rest/api/2/issue/search?jql=project=XXXXX%20and%20summary%20~%20%22Testing%20Jira%20Integration%20Pass%22&fields=id,key,summary
2016-05-26 06:27:55 DEBUG - Http Response Code: 404

1 Answer 1

1

I had it all wrong. I had to build the request into a JSON string, once i did that, and used the correct URL, and did POST, it worked.

String url = buildUrl("jira/rest/api/2/search");
    String query = "project = XXXX AND issuetype = Test AND text ~ \"" + testId + "\"";

JSONArray fields = new JSONArray();
fields.put("id");
fields.put("key");
fields.put("summary");

JSONObject reqObj = new JSONObject();
reqObj.put("jql", query);
reqObj.put("startAt", 0);
reqObj.put("maxResults", 15);
reqObj.put("fields", fields);

String requestBody = reqObj.toString();
logger.debug("Request body: " + requestBody);

HttpURLConnection conn = httpPost(url, null, null, requestBody);
logger.debug("HTTP response code: " + conn.getResponseCode());
String response = IOUtils.toString(conn.getInputStream());
logger.debug("HTTP res content: " + response);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.