Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Using JIRA REST API within Java project with cURL

Rick de Jong January 3, 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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Timothy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 4, 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 January 4, 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 January 4, 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 January 4, 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 January 4, 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 July 3, 2016

Thank you so much. It works for me.

TAGS
AUG Leaders

Atlassian Community Events