-2

I have the following curl command which gives a JSON response:

curl --globoff --insecure --silent -u username:password -X GET -H 'Content-Type: application/json' "http://ficcjira.xyz.com/rest/api/2/search?jql=project=ABC&fields=Timetracking"

I want to replicate this in Java. Could anyone tell me how to do that?

user3591433
  • 105
  • 1
  • 3
  • 11

2 Answers2

0

You can use this code

String url = "whatever.your.url.is";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();

connection .setRequestProperty("Content-Type", "application/json");
connection .setRequestMethod("POST");
JSONObject json =new JSONObject();
JSONObject skills =new JSONObject();
skills.put("__op", "AddUnique");
skills.put("objects", new JSONArray(Arrays.asList("flying", "kungfu"));
json.put("skills": skills);
OutputStreamWriter wr= new OutputStreamWriter(connection.getOutputStream());
wr.write(json.toString());

Resource Link:

Java: how to use UrlConnection to post request with authorization?

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • The question is clearly doing a Get request, not a Post request. Also, your code is missing the `connection.setRequestProperty("Authorization", "Basic " + encodedString);` call that actually supplies the authorization information, as shown in the question to which you’ve linked. – VGR Jun 15 '16 at 16:41
  • @user3591433 all are skills. – SkyWalker Jun 15 '16 at 17:30
  • @SkyWalker I don't understand how this is a conversion of my curl. As VGR mentioned, my curl is doing a get request. Plus where do I do authentication? – user3591433 Jun 15 '16 at 17:35
0

You’ll want to look at these:

Essentially, you need to set the "Authorization" header in the request to “Basic base64” where base64 is the user and password, separated by a colon, encoded in base 64.

URL url = new URL("http://ficcjira.xyz.com/rest/api/2/search?jql=project=ABC&fields=Timetracking")
URLConnection conn = url.openConnection();

String auth = user + ":" + password;
byte[] authBytes = auth.getBytes(StandardCharsets.UTF_8);
String encodedAuth = Base64.getEncoder().encodeToString(authBytes);
conn.setRequestProperty("Authorization", "Basic " + encodedAuth);

try (InputStream responseStream = conn.getInputStream()) {

    // To read response as a string:
    //MimeType contentType = new MimeType(conn.getContentType());
    //String charset = contentType.getParameter("charset");
    //String response =
    //    new Scanner(responseStream, charset).useDelimiter("\\Z").next();

    // To save response to a file:
    //Path response = Files.createTempFile(null, null);
    //Files.copy(responseStream, response,
    //    StandardCopyOption.REPLACE_EXISTING);

    // To read as JSON object using javax.json library:
    //JsonObject response =
    //    Json.createReader(responseStream).readObject();
}
Community
  • 1
  • 1
VGR
  • 40,506
  • 4
  • 48
  • 63
  • Thanks a lot. This is very helpful. However I'm getting unable to find valid certification path to requested target error. Why is this so? – user3591433 Jun 15 '16 at 18:49
  • You are connecting to an `https:` URL whose server is using a self-signed or mismatched certificate, which is invalid since it doesn’t securely guarantee the identity of the server. See http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate for solutions and workarounds. – VGR Jun 15 '16 at 20:02
  • I'm getting error here: String contentType = new MimeType(conn.getContentType()); String charset = contentType.getParameter("charset"); Type mismatch: cannot convert from MimeType to String – user3591433 Jun 15 '16 at 20:02
  • YEs, I figured that out. Please ignore my previous comment. – user3591433 Jun 15 '16 at 20:02
  • `String contentType` should be `MimeType contentType`. I’ve updated my answer accordingly. – VGR Jun 15 '16 at 20:04