It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Using JIRA REST API within Java project with cURL

Hello!

I'm trying to setup a connection between a Java project of mine and my self hosted JIRA instance. I actually never worked with cURL before so I'm kind of at a loss as to how to use it. The documentation seems pretty straightforward and I'm confident that I should be able to figure that out on my own but I can't seem to wrap my head around the setup of cURL in a Java project.

I've tried places like Stackoverflow and Google but no dice. So this question is pretty straightforward and shows how green I am but how do I setup the usage of cURL inside my Java project?

My goal is so I can create/change/delete issues from within my Java project (which is a monitoring service) and I need the cURL command for this (if I understand the documentation correctly). So can anyone help me with this? Or if you have a better way of doing this then by all means elaborate on it.

Thank you and sorry for the rookie question! 

2 answers

1 accepted

1 vote
Answer accepted
Timothy Chin Community Leader Jan 04, 2016

cURL is more for scripting on the command line. You already have a full fledged language. Use Apache's http libraries or something similar to call the REST APIs in JIRA. There's even a JIRA REST API client:

 

Hi Timothy! Thanks for answering. I tried using the HttpURLConnection but it isn't working just yet. Hopefully you can help me with that. try { //store necessary query information URL jiraURL = new URL("https://jirainstance.atlassian.net/rest/api/2/issue";;); String data = "{\"jql\":\"project = TP\"}"; String login = "username:password"; final String encoded = Base64.getEncoder().encodeToString(dataBytes); //establish connection and request properties connection = (HttpURLConnection) jiraURL.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Accept", "*/*"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.setUseCaches(false); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.write(dataBytes); wr.flush(); wr.close(); Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); for (int c; (c = in.read()) >= 0; System.out.print((char)c)); }

and I got received a 401 unauthorized. I know that my username and password is correct but I'm making an error somewhere but I can't see it

I managed to fix the problem! It created an issue without problem. Its the small achievements that make you happy in life!

For anyone who is looking for a Java way of creating issues. Please use this. I highly doubt that this is the most efficient way of doing it (I still need to fix my code to make it more readable and stuff) but hopefully this can help you! Also look at Timothy's answers if you come here to look for options to create issues via JIRA REST API (Like I was).

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Run {
    public static void main(String[] args)
    {
        HttpURLConnection connection = null;
        try {
            //store necessary query information
            URL jiraURL = new URL("https://jirainstance.atlassian.net/rest/api/2/issue");
            String data = "{\n" +
                    "    \"fields\": {\n" +
                    "       \"project\":\n" +
                    "       { \n" +
                    "          \"key\": \"TP\"\n" +
                    "       },\n" +
                    "       \"summary\": \"REST ye merry gentlemen.\",\n" +
                    "       \"description\": \"Creating of an issue using project keys and issue type names using the REST API\",\n" +
                    "       \"issuetype\": {\n" +
                    "          \"name\": \"Bug\"\n" +
                    "       }\n" +
                    "   }\n" +
                    "}";
            byte[] dataBytes = data.getBytes("UTF-8");
            String login = "username:password";
            final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
            String encoded = Base64.getEncoder().withoutPadding().encodeToString(authBytes);


        //establish connection and request properties
        connection = (HttpURLConnection) jiraURL.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept", "*/*");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Authorization", "Basic " + encoded);
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);

        connection.connect();

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.write(dataBytes);
        wr.flush();
        wr.close();

        Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
        for (int c; (c = in.read()) >= 0; System.out.print((char)c));

    } catch (MalformedURLException e) {
            System.err.println("MalformedURLException: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());

            if (connection != null) {
                try {
                    System.out.println(connection.getResponseMessage());

                    InputStream errorStream = connection.getErrorStream();
                    if (errorStream != null) {
                        Reader in = new BufferedReader(new InputStreamReader(errorStream));
                        for (int c; (c = in.read()) >= 0; System.out.print((char) c));
                    }
                } catch(IOException e2) {}
            }
        }
    }
}

Thank you so much. It works for me.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published Thursday in Confluence

Confluence CVEs and common questions

Two vulnerabilities have been published for Confluence Server and Data Center recently: March 20, 2019 CVE-2019-3395 / CVE-2019-3396 April 17, 2019 CVE-2019-3398 The goal of this article is...

144 views 0 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you