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

Rick de Jong Jan 03, 2016

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:

 

Rick de Jong Jan 04, 2016

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)); }

Rick de Jong Jan 04, 2016

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

Rick de Jong Jan 04, 2016

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

1 vote
Rick de Jong Jan 04, 2016

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) {}
            }
        }
    }
}
Prashant Mali Jul 03, 2016

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 in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

10,654 views 49 52
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you