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

Create issue in jira using java program Edited

I want to create issue in existing project in jira using java program. please suggest me how to do.

 

1 answer

1 accepted

0 votes
Answer accepted

Hi Vineet,

You should have a look at the following Developer Resource: Java APIs. Or have a look at an older question which could give you something to start: Create issue via REST API useing Java

Hope this helps.

Best,

Maarten

In the above link attached program i am getting 401 error

Hi Vineet,

401 means something is not right with the authentication. Have a look at the following line of code and make sure to adjust it to your credentials:

conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vzverev:Gromozavr".getBytes(), 0)); 

Best,

Maarten

inputstream variable is not used

package NewPost;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import java.io.*;
import java.net.*;
import javax.json.*;

public class testREST_CreateIssue {
public static void main(String[] args) {
try {
URL jiraREST_URL = new URL("http://localhost:8080/rest/api/2/issue/");
URLConnection urlConnection = jiraREST_URL.openConnection();
urlConnection.setDoInput(true);

HttpURLConnection conn = (HttpURLConnection) jiraREST_URL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");

System.out.println(getJSON_Body() + "/" + encodedData);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vineet1492:742762".getBytes(), 0));
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
conn.getOutputStream().write(encodedData.getBytes());

try {
InputStream inputStream = conn.getInputStream();
}
catch (IOException e){
System.out.println(e.getMessage());
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static String getJSON_Body(){
JsonObject createIssue = Json.createObjectBuilder()
.add("fields",
Json.createObjectBuilder().add("project",
Json.createObjectBuilder().add("key","USC"))
.add("summary", "sum")
.add("description", "descr")
.add("issuetype",
Json.createObjectBuilder().add("id", "10105"))
).build();

return createIssue.toString();

}
}

 

if i remove inputstream then it gives error at 

conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vineet1492:742762".getBytes(), 0));

Still getting error 400 in the code.

 

import java.io.*;
import java.net.*;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import javax.json.*;

 

public class testREST_POST_CreateIssues {
public static void main(String[] args) {
try {
URL jiraREST_URL = new URL("http://localhost:8080/rest/api/2/issue/");

/* Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass.toCharArray());
}
});
*/
URLConnection urlConnection = jiraREST_URL.openConnection();
urlConnection.setDoInput(true);

HttpURLConnection conn = (HttpURLConnection)jiraREST_URL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");

System.out.println(getJSON_Body() + "/" + encodedData);
System.out.println(getJSON_Body());
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("username:password".getBytes(),0));
conn.setRequestProperty("Content-Type", "application/json");
System.out.println(conn);
conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
conn.getOutputStream().write(encodedData.getBytes());

try {
InputStream inputStream = conn.getInputStream();
System.out.println(inputStream);
}
catch (IOException e){
System.out.println(e.getMessage());
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

private static String getJSON_Body(){
JsonObject createIssue = Json.createObjectBuilder()
.add("fields",
Json.createObjectBuilder().add("project",
Json.createObjectBuilder().add("key","USC"))
.add("summary", "sum")
.add("description", "descr")
.add("issuetype",
Json.createObjectBuilder().add("id", "10015"))
).build();

return createIssue.toString();

}
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Calling all Jira Cloud users! Give us feedback on our exploration of a new navigation.

Hi everyone! My name’s Matt and I’m a product manager at Atlassian. I work in the navigation & findability space for all our Jira Cloud products. We’ve been working on trying to improve the exp...

1,058 views 16 12
Join discussion

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