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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published Apr 09, 2019 in Portfolio for Jira

Portfolio for Jira 3.0 is here!

The wait is over... Portfolio for Jira Server and Data Center 3.0 is now officially here! Platform releases offer Atlassian an opportunity to shift our strategy, make bold predictions about t...

1,267 views 12 24
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